You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
JavaScript is a feature-rich language. It has a lot of features that are not well known. In this article, I will show you JavaScript tricks that you've never heard about. Let's Explore some of them:
1. Destructuring
With the help of destructuring, you can do awesome things like:
1.1. Swap variables
If you ever need to swap two variables. You can do it with the help of destructuring, provided that the variables are declared with let, or var but not const.
Also, you should be using ES6 or above.
leta=1;letb=2;[a,b]=[b,a];console.log(a,b);// 2 1
1.2. Accessing nested objects
Previously, if you wanted to access a nested object, you would have to do something like this:
But now, with the help of destructuring, you can do it in a much simpler way:
constobj={a: {b: {c: 1}}};const{a: {b: { c }}}=obj;console.log(c);// 1const{a: {b: {c:value}}}=obj;// and yes, you can rename the variableconsole.log(value);// 1
1.3. Pass an object as function parameters
If you want to pass different parameters to a function, why not pass an object instead? It will make your code more readable. and you can use destructuring to access the properties of the object.
functionfoo({ a, b, c }){console.log(a,b,c);}foo({a: 1,b: 2,c: 3});// 1 2 3
2. Spread & Rest Operators
2.1. Spread Operator
The spread operator ... is used to spread an array into its elements. It can also be used to spread an object into key-value pairs.
constarr=[1,2,3];console.log(...arr);// 1 2 3constobj={a: 1,b: 2,c: 3};console.log(...obj);// a b c
2.1.1. Spread in function calls
You can use the spread operator to pass an array as arguments to a function.
The rest operator ... is used to collect multiple elements and put them into an array. It can also be used to collect multiple key-value pairs and put them into an object.
The ternary operator is the only operator that takes three operands. It is often used as a shortcut for the if statement.
consta=1;constb=2;constc=a>b ? a : b;console.log(c);// 2
4. Template Literals
Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them.
constname="John";constage=20;conststr=`Hello, my name is ${name}I'm ${age} years old.`;console.log(str);// Hello, my name is John // I'm 20 years old.
5. Short-circuiting
Short-circuiting is a way to make your code more readable. It is a way to use logical operators in a way that they return the value of one of the specified operands, instead of always returning true or false.
5.1. Logical AND (&&)
The logical AND operator (&&) returns the value of the first operand if it can be converted to false; otherwise, it returns the value of the second operand.
consta=1;constb=2;constc=a&&b;console.log(c);// 2
5.2. Logical OR (||)
The logical OR operator (||) returns the value of the first operand if it can be converted to true; otherwise, it returns the value of the second operand.
consta=1;constb=2;constc=a||b;console.log(c);// 1
5.3. Nullish Coalescing Operator (??)
The nullish coalescing operator (??) returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.
The main difference between || and ?? is that || returns the right-hand side operand if the left-hand side operand is false, whereas ?? returns the right-hand side operand if the left-hand side operand is null or undefined.
consta=1;constb=2;constc=a??b;console.log(c);// 1
6. Immediately Invoked Function Expression (IIFE)
An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. One of the most common uses of an IIFE is to execute a function without polluting the global scope.
With the optional chaining operator (?.), you can access properties of nested objects without having to check if the parent objects exist. If the reference is undefined or null, the expression short-circuits with a return value of undefined.
The double bang operator (!!) is used to convert any value to a boolean. It is often used to convert a value to a boolean in a shorter way.
consta=1;constb=!!a;console.log(b);// true
10. Double Tilde (~~) Operator
The double tilde operator (~~) is used to convert any value to an integer. It is often used to convert a value to an integer in a shorter way.
consta=1.5;constb=~~a;console.log(b);// 1
Thank you so much for taking the time to read the blog post! I appreciate it, and I hope you found it helpful/informative. If you have any questions or comments, feel free to leave them below, and I’ll be happy to answer them. Again, thank you for reading, and have a great day!
The text was updated successfully, but these errors were encountered:
description: JavaScript is a feature-rich language. It has a lot of features that are not well known. In this article, I will show you JavaScript tricks that you've never heard about.
category: blog
slug: javascript-tricks-part-1
canonical: https://blog.blackkspydo.com/javascript-tricks-part-1
image: https://user-images.githubusercontent.com/58302072/195398055-7c41944e-8978-4fff-a18f-8a1a7e235519.jpg
JavaScript is a feature-rich language. It has a lot of features that are not well known. In this article, I will show you JavaScript tricks that you've never heard about. Let's Explore some of them:
1. Destructuring
With the help of destructuring, you can do awesome things like:
1.1. Swap variables
If you ever need to swap two variables. You can do it with the help of destructuring, provided that the variables are declared with
let
, orvar
but notconst
.Also, you should be using ES6 or above.
1.2. Accessing nested objects
Previously, if you wanted to access a nested object, you would have to do something like this:
But now, with the help of destructuring, you can do it in a much simpler way:
1.3. Pass an object as function parameters
If you want to pass different parameters to a function, why not pass an object instead? It will make your code more readable. and you can use destructuring to access the properties of the object.
2. Spread & Rest Operators
2.1. Spread Operator
The spread operator
...
is used to spread an array into its elements. It can also be used to spread an object into key-value pairs.2.1.1. Spread in function calls
You can use the spread operator to pass an array as arguments to a function.
2.1.2. Spread in array literals
You can use the spread operator to create a new array from an existing array.
2.1.3. Spread in object literals
You can use the spread operator to create a new object from an existing object.
2.2. Rest Operator
The rest operator
...
is used to collect multiple elements and put them into an array. It can also be used to collect multiple key-value pairs and put them into an object.3. Ternary Operator
The ternary operator is the only operator that takes three operands. It is often used as a shortcut for the
if
statement.4. Template Literals
Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them.
5. Short-circuiting
Short-circuiting is a way to make your code more readable. It is a way to use logical operators in a way that they return the value of one of the specified operands, instead of always returning
true
orfalse
.5.1. Logical AND (&&)
The logical AND operator (
&&
) returns the value of the first operand if it can be converted tofalse
; otherwise, it returns the value of the second operand.5.2. Logical OR (||)
The logical OR operator (
||
) returns the value of the first operand if it can be converted totrue
; otherwise, it returns the value of the second operand.5.3. Nullish Coalescing Operator (??)
The nullish coalescing operator (
??
) returns its right-hand side operand when its left-hand side operand isnull
orundefined
, and otherwise returns its left-hand side operand.The main difference between
||
and??
is that||
returns the right-hand side operand if the left-hand side operand isfalse
, whereas??
returns the right-hand side operand if the left-hand side operand isnull
orundefined
.6. Immediately Invoked Function Expression (IIFE)
An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined. One of the most common uses of an IIFE is to execute a function without polluting the global scope.
7. Comma Operator
The comma operator (
,
) is used to evaluate each of its operands (from left to right) and return the value of the last operand.8. Optional Chaining
Previously, if you wanted to access a property of an object that might be
undefined
ornull
, you would have to check if the object exists first.For example:
With the optional chaining operator (
?.
), you can access properties of nested objects without having to check if the parent objects exist. If the reference isundefined
ornull
, the expression short-circuits with a return value ofundefined
.9. Double Bang (!!) Operator
The double bang operator (
!!
) is used to convert any value to a boolean. It is often used to convert a value to a boolean in a shorter way.10. Double Tilde (~~) Operator
The double tilde operator (
~~
) is used to convert any value to an integer. It is often used to convert a value to an integer in a shorter way.Thank you so much for taking the time to read the blog post! I appreciate it, and I hope you found it helpful/informative. If you have any questions or comments, feel free to leave them below, and I’ll be happy to answer them. Again, thank you for reading, and have a great day!
The text was updated successfully, but these errors were encountered: