Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 4, 2022 08:55 am GMT

5 cool JavaScript features that most developers don't know about

You can use JavaScript to do the same thing in different ways. With the release of each new ECMAScript specification, new methods and operators are added to make the code shorter and more readable.

Code

1. Object.entries

Most developers use the Object.keys method to iterate through an object. This method returns only an array of object keys, not values. You can use Object.entries to get both the key and the value.

const person = {  name: 'Nick',  age: 27};Object.keys(person); // ['name', 'age']Object.entries(data); // [['name', 'Nick'], ['age', 27]]

To iterate over an object, we can do the following::

Object.keys(person).forEach((key) => {  console.log(`${key} is ${person[key]}`);});// using records to get the key and valueObject.entries(person).forEach(([key, value]) => {  console.log(`${key} is ${value}`);});// expected result:// name is Nick// age is 27

Both approaches described above return the same result, but Object.entries makes it easy to get a key-value pair.

2. The replaceAll method

In JavaScript, to replace all occurrences of a string with another string, we need to use a regular expression like the following:

const str = 'Red-Green-Blue';// replaces only the first entrystr.replace('-', ' '); // Red Green-Blue// use a regular expression to replace all entriesstr.replace(/\-/g, ' '); // Red Green Blue

But in ES12, a new replaceAll method was added to String.prototype, which replaces all occurrences of the string with another string value:

str.replaceAll('-', ' '); // Red Green Blue

3. Numeric separator

You can use the underscore "_" as a numeric separator to simplify counting the number of zeros in a number.

// less readableconst billion = 1000000000;// more readableconst readableBillion = 1000_000_000;console.log(readableBillion) // returns 1000000000

The separator can also be used with BigInt numbers, as in the following example:

const trillion = 1000_000_000_000n;console.log(trillion); // 1000000000000

This makes the number more readable.

4. document.designMode

Linked to front-end JavaScript, design Mode lets you edit any content on the page. Just open the browser console and enter the following:

document.designMode = 'on';

This is useful for designers, as they don't need to change something in the code every time to match the changes on the screen.

5. Logical assignment operator

Logical assignment operators are a combination of the logical operators &&, ||, ?? and the assignment operator =.

const a = 1;const b = 2;a &&= b;console.log(a); // returns 2// the above statement is equivalent to a && (a = b);// OR another wayif (a) {  a = b}

Here it checks whether the value of a matches true, and if so, we update its value. The same can be done with the logical OR // operator.

const a = null;const b = 3;a ||= b;console.log(a); // returns 3// the above statement is equivalent toa || (a = b);

And also with the help of an operator ??:

const a = null;const b = 3;a ??= b;console.log(a); // returns 3// the above statement is equivalent toif (a === null || a === undefined) {  a = b;}

The operator ?? checks only for null or undefined values.

Note that logical assignment operators have been added since ES 12/ES 2021.

Conclusion

These tricks and features can speed up the developer's work, and their use is not only necessary, but also useful. Continue to explore the hidden features of the language, learn all sorts of tricks and improve your skills.


Original Link: https://dev.to/ra1nbow1/5-cool-javascript-features-that-most-developers-dont-know-about-5b7f

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To