Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Front End Technology Articles
Page 390 of 652
Execute digits in even places in a JavaScript array?
In JavaScript, extracting elements from even positions in an array requires understanding that array indexing starts from 0. Elements at even positions are at indices 0, 2, 4, etc., while elements at odd positions are at indices 1, 3, 5, etc. To extract elements from even positions, we can use various methods like filter(), traditional loops, or specific iteration patterns. Understanding Array Positions JavaScript arrays are zero-indexed, meaning: Even positions: indices 0, 2, 4, 6... Odd positions: indices 1, 3, 5, 7... Using the filter() Method The filter() method creates a new array ...
Read MoreSuper keyword in JavaScript?
In this article, we are going to discuss about the super keyword in JavaScript with suitable examples. The super keyword is used in JavaScript classes to access properties and methods of a parent class from a child class. It's essential for implementing proper inheritance in object-oriented programming. When a child class and parent class have methods with the same names, the super keyword helps distinguish between them. The child class must extend the parent class using the extends keyword to use super. Syntax The syntax to represent the super keyword is: super(arguments); ...
Read MoreCheck whether a string ends with some other string - JavaScript
In JavaScript, there are multiple ways to check if a string ends with another string. The most straightforward approach is using the built-in endsWith() method, though you can also implement custom solutions. Using String.endsWith() (Recommended) The endsWith() method is the standard way to check if a string ends with a specific substring: const str1 = 'this is just an example'; const str2 = 'ample'; console.log(str1.endsWith(str2)); // true console.log(str1.endsWith('temple')); // false console.log(str1.endsWith('example')); // true true false true Custom Implementation ...
Read MoreHow to generate a random number in JavaScript?
JavaScript's Math.random() method is the built-in way to generate random numbers. It returns a floating-point number between 0 (inclusive) and 1 (exclusive), which can be scaled to any desired range. The Math.random() function returns a pseudo-random number in the range [0, 1) with uniform distribution. The implementation generates the seed automatically, and users cannot modify it. Basic Math.random() Usage Syntax Math.random() Return Value A floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive). Example Random Number Generation ...
Read MoreDeleting the duplicate strings based on the ending characters - JavaScript
We are required to write a JavaScript function that takes in an array of strings and deletes each one of the two strings that ends with the same character. For example, if the actual array is: const arr = ['Radar', 'Cat', 'Dog', 'Car', 'Hat']; Then we have to delete duplicates and keep only one string ending with the same character. In this case, 'Cat', 'Car', and 'Hat' all end with 't', 'r', and 't' respectively, so we need to remove duplicates based on the last character. How It Works The algorithm uses a ...
Read MoreJavaScript's Boolean function?
The Boolean() function in JavaScript converts any value to its boolean equivalent (true or false). This is useful for conditional checks and validation in your applications. Syntax Boolean(value); It takes any value or expression and returns true for truthy values and false for falsy values. Falsy Values These values always return false when converted to boolean: console.log(Boolean(0)); // false console.log(Boolean("")); // false (empty ...
Read MoreSwap certain element from end and start of array - JavaScript
We need to write a JavaScript function that accepts an array of numbers and a position k, then swaps the kth element from the beginning with the kth element from the end of the array. Understanding the Problem For an array with indices 0 to n-1, the kth element from start is at index k-1, and the kth element from end is at index n-k. We swap these two elements. Example Implementation const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; const swapNth = (arr, k) => { ...
Read MoreHow to access a JavaScript object using its own prototype?
JavaScript's Object.create() method creates a new object with the specified object as its prototype. This allows the new object to inherit properties from the existing object while maintaining a prototype chain. Syntax Object.create(prototypeObject); This method takes an existing object and creates a new object that inherits properties from it through the prototype chain. How It Works When you create an object using Object.create(), the new object doesn't copy the properties directly. Instead, it creates a prototype link to the original object, allowing property inheritance. var ...
Read MoreShift certain array elements to front of array - JavaScript
We are required to write a JavaScript function that takes in an array of numbers. The function should bring all the 3-digit integers to the front of the array. Let's say the following is our array of numbers: const numList = [1, 324, 34, 3434, 304, 2929, 23, 444]; Understanding 3-Digit Numbers A 3-digit number is any integer between 100 and 999 (inclusive). We can check this using a simple condition: const isThreeDigit = num => num > 99 && num < 1000; console.log(isThreeDigit(324)); // true console.log(isThreeDigit(34)); // ...
Read MoreWhy is using "for...in" loop in JavaScript array iteration a bad idea?
The for...in loop in JavaScript is designed for iterating over object properties, not array elements. Using it for arrays can lead to unexpected behavior and performance issues. Main Problems with for...in on Arrays There are several critical issues when using for...in loops with arrays: Prototype pollution: If Array.prototype is modified, for...in will iterate over inherited properties, not just array elements. No guaranteed order: for...in doesn't guarantee that array elements will be processed in numerical order. Performance overhead: The loop checks the entire prototype chain, making it slower ...
Read More