You are on page 1of 7

Top 10 JavaScript

Short Hand Tricks

Part - 1

Every Developer
Must Know

Paras Mayur
Ternary Operator

We can use the ternary operator to write a


simple if-else statement in one line.

Example: let x = (a > b) ? "a is greater" : "b is


greater";

Short-circuit evaluation

We can use the logical AND (&&) and OR (||)


operators to perform short-circuit evaluations.

Example: let x = (a > b) && "a is greater" || "b is


greater";
Scan & Join

Paras Mayur FrontEnd Community


Object Property Shorthand

When creating an object, we can use


shorthand notation for properties that
have the same name as variables.

Example: let x = 5, y = 10; let obj = {x, y};


Paras Mayur

Destructuring Assignment

We can use destructuring assignment to


extract values from arrays or objects
and assign them to variables.

Example: let arr = [1, 2, 3]; let [x, y, z] =


arr;
Spread Operator

We can use the spread operator (...) to spread


the elements of an array or object into a new
array or object.

Example: let arr1 = [1, 2, 3]; let arr2 = [...arr1, 4,


5];
Paras Mayur

Array.map() Method

We can use the map() method to create a


new array with the results of calling a
provided function on every element in the
calling array.

Example: let arr = [1, 2, 3]; let newArr =


arr.map(x => x * 2);
Paras Mayur
Array.filter() Method

We can use the filter() method to create a


new array with all elements that pass the test
implemented by the provided function.

Example: let arr = [1, 2, 3, 4, 5]; let newArr =


arr.filter(x => x % 2 === 0);

Array.reduce() Method

We can use the reduce() method to apply a


function to each element in an array and
reduce it to a single value.

Example: let arr = [1, 2, 3, 4, 5]; let sum =


arr.reduce((acc, val) => acc + val, 0);

Paras Mayur
Arrow Functions

We can use arrow functions to create shorter,


more concise function expressions.

Example: let sum = (a, b) => a + b;

Paras Mayur

Template Literals

We can use template literals (backticks) to


create strings that contain expressions.

Example: let x = 5, y = 10; console.log(The


sum of ${x} and ${y} is ${x + y}.);

Paras Mayur
@ParasMayur

Was it helpful?

Like Comment Share Save

Paras Mayur
Scan & Join

FrontEnd Community

You might also like