THAPA TECHNICAL

HOUSE OF WEB DEVELOPERS AND TECHNOLOGY.

JavaScript Top 5 One Liners Codes Every Developers Must Use


JavaScript is a powerful and versatile programming language, and developers often use one-liner codes to save time and improve their workflow. 


1: Use arrow functions to write concise anonymous functions. 

Arrow functions, also known as "fat arrow" functions, are a more concise way of writing anonymous functions in JavaScript. They use the "fat arrow" (=>) syntax to define the function and automatically bind the value of this to the surrounding context. 

let square = x => x * x; console.log(square(4)); // Output: 1


2: To check if all elements in an array satisfy a given predicate

In JavaScript, you can use the Array.prototype.every() method to check if all elements in an array satisfy a given predicate. The every() method takes a callback function as its argument, which is called for each element in the array. If the callback function returns true for all elements in the array, the every() method returns true. 


let arr = [2, 4, 6, 8, 10]; let result = arr.every(x => x % 2 === 0); console.log(result); // Output: true


3: Use the spread operator to concatenate arrays

The spread operator (...) in JavaScript can be used to concatenate arrays by spreading the elements of one array into another. Here is an example of how to use the spread operator to concatenate two arrays:


let arr1 = [1, 2, 3]; let arr2 = [4, 5, 6]; let concatenatedArray = [...arr1, ...arr2]; console.log(concatenatedArray); // Output: [1, 2, 3, 4, 5, 6]


In this example, the spread operator spreads the elements of arr1 and arr2 into a new array, concatenatedArray. The elements of arr1 are spread first, followed by the elements of arr2, resulting in a single array that contains all elements from both original arrays.


4: A function that takes an array and returns a new array with all elements squared

You can use the Array.prototype.map() method in combination with an arrow function to return a new array with all elements squared. The map() method creates a new array with the results of calling a provided function on every element in the calling array. Here is an example:


let squareArray = arr => arr.map(x => x*x); console.log(squareArray([1,2,3])); // Output: [1,4,9]


In this example, the function squareArray takes an array as an argument, and it uses the map() method to create a new array, by applying the arrow function to each element in the original array. The arrow function takes an element x as an input, and returns the square of x as the output.


5: Use the Array.prototype.includes method to check if an array contains a value

In JavaScript, you can use the Array.prototype.includes() method to check if an array contains a specific value. The includes() method returns a Boolean value indicating whether the given value can be found in the array. Here is an example of how to use the includes() method to check if an array contains a specific value:


let arr = [1, 2, 3, 4, 5]; let result = arr.includes(3); console.log(result); // Output: true


In this example, the includes() method is called on the arr array and is passed the value 3 as an argument. The method checks if the value 3 can be found in the arr array and returns true if it can. If the value is not found, the method returns false.




5: Use the Array.prototype.includes method to check if an array contains a value


In JavaScript, you can use the Array.prototype.includes() method to check if an array contains a specific value. The includes() method returns a Boolean value indicating whether the given value can be found in the array. Here is an example of how to use the includes() method to check if an array contains a specific value:


In this example, the includes() method is called on the arr array and is passed the value 3 as an argument. The method checks if the value 3 can be found in the arr array and returns true if it can. If the value is not found, the method returns false.


6: Use template literals to simplify string concatenation


7: Use the Array.prototype.filter method to create a new array with only certain elements from an existing array


8: Use the Array.prototype.some method to check if at least one element in an array satisfies a given predicate


9: Convert seconds to hh:mm:ss format


10: Removing duplicates from an array

This code creates a new Set object, which automatically removes duplicates, and then uses the spread operator to convert it back into an array.


These one-liner codes can be used to accomplish common tasks quickly and efficiently, helping developers to save time and improve their workflow.

No comments:

Post a Comment