DEV Community

loading...
Cover image for New Features of JavaScript 2021

New Features of JavaScript 2021

Animesh Dhanuk
PHP Developer with 8+ years of experience.
・3 min read

Nowadays we should know about some programming languages like C language, C++, CSS, JavaScript, Python, etc. Today we discuss new features of JavaScript that are very useful for programmers/ coders/ developers.

JavaScript is the easiest programming language to learn for beginners. You can learn it from the tutorial by yourself.JavaScript is the front-end programming language.JavaScript gives some new features in 2021 that are very helpful for developers/ coders/ programmers. A few of the features of JavaScript given below:

1. Logical Operators

JavaScript have AND, OR, NOT operators but in new JavaScript updated three new logical operators are as follow:

A. &&= Operator

In the new logical operator if X variable has a specific value, then the variable should be assigned the value of variable Y. That's why we use a console.use(X), now the value of the variable of X is a change from 10 to 15.Let see the example:

    let X = 10;
    let Y = 15;
    X &&= Y;
    console.use(X);
    // Now, the output of variable X is 15.
Enter fullscreen mode Exit fullscreen mode

B. ||= Operator

This operator is the opposite of &&= operator. In this case value of variable X is not change. If variable X has the wrong value then variable X and variable Y will be equal. Let see the example:

let X = 10;
let Y = 15;
X &&= Y;
console.use(X);
// Now, the output of variable X is 10.
Enter fullscreen mode Exit fullscreen mode

C. ??= Operator

This operator is used to check the value of the variable is NULL or not. Let see the example:

    let X;
    let Y = 15;
    X ??= 10;
    console.use(X);
    // Now, the output of variable X is 10.

Logic of this operator:
    if(X == NULL ||  X == undefined){
    X = 10
    };
Enter fullscreen mode Exit fullscreen mode

2. replaceAll method using String

We all use replaceAll method using string. The new JavaScript replace method to have a limitation to change the word in a string. It changes only one word at a time.If you want to replace all words at a time you can use the regular expression. Now let see the example:

// with regex
let str = "Hello World!, Hello Their;"
console.log(str.replace(/Hello/g, "Hii"));
// Output will be "Hii World!, Hii Their;"

// without regex
let str = "Hello World!, Hello Their;"
console.log(str.replace('Hello', 'Hii'));
// output will be"Hii World!, Hello Their;"
Enter fullscreen mode Exit fullscreen mode

3. Using underscores for integers as a separator

Sometimes Integers are used as a data type in a string and array. It's very difficult to find out the perfect number of elements that are in million or billion.

But now in the latest JavaScript, we can easily find the number with the help of underscores(_). We can use underscores as separators in the integer. Let see the example below:

    let number = 1_000_000; // one million
    console.log(number);
    // output will be 1000000(the number will remain an integer)
Enter fullscreen mode Exit fullscreen mode

4. Promise.any()

Promise.any() is a new function in JavaScript.Promise.any() takes an iterable of Promise objects and, as soon as one of the promises in the iterable fulfills, returns a single promise that resolves with the value from that promise. If no promises in the iterable fulfill, then the returned promise is rejected with an AggregateError, a new subclass of Error that groups together individual errors. This method is opposite from promis.all(). Let see the example:

        const promise1 = Promise.reject(0);
        const promise2 = new Promise((resolve) => setTimeout(resolve, 25, 'Even'));
        const promise3 = new Promise((resolve) => setTimeout(resolve, 30, 'Odd'));
        const promises = [promise1, promise2, promise3];
        Promise.any(promises).then((value) => console.log(value));
        // output will be "Even" 
Enter fullscreen mode Exit fullscreen mode

Read More

Discussion (2)

Collapse
pcockerell profile image
Peter Cockerell • Edited

I feel I understand some of these topics less well than I did before reading your explanations. Your sections on &&= and ||= are particularly bad and really don't explain that they're essentially shortcuts for x = x && y and x = x || y respectively, with y only being evaluated if x is true (or false, respectively).

Your example for the new form of replaceAll() didn't actually use the string version of the function!

I think readers would be better served by simply posting a link to the appropriate pages on developer.mozilla.org, from where your explanation of Promise.any() seems to have been copied almost verbatim.

ETA: it's actually more accurate to say the equivalent of x &&= y is x && (x = y) because this emphasizes that the assignment won't take place at all if !x. In my original equivalent, x = x && y, there was an implication that x = x is executed if !x, and of course that would be silly. Similar argument for ||=.

Collapse
akhilgautam profile image
Akhil • Edited

The code block for 1A and 1B looks same. Isn't there a typo in the operator?