Helder Esteves

Type and hit Enter to search

JavaScript
Uncategorized
Software Architecture
React
General
Management
Next.JS
TypeScript
JavaScript
Uncategorized
Software Architecture
React
General
Management
Next.JS
TypeScript
opened secret door inside library
JavaScript

5 JavaScript Features You Didn’t Know About

Helder Esteves
Helder Esteves
March 6, 2023 2 Mins Read
70 Views
0 Comments

Whether you’re a new or an experienced developer, these are not-so-well-known JavaScript features that you may have missed out on or forgotten about, as they’re not standard practice.

However, it’s always good to know these to increase your mental dexterity and your JS prowess.

Nullish coalescing operator

The nullish coalescing operator (??) is basically the opposite of the OR (||) logical operator. It returns the right-hand side operand only if the left-hand side is either null or undefined

const x = null;
console.log(x ?? "Hey"); // "Hey"

const y = 10;
console.log(y ?? "Something"); // 10

You can also use the logical nullish assignment (??=) for assigning a variable only if that variable is either null or undefined. For example:

let a = null;
a ??= 10;
console.log(a); // "10"

let b = "something";
b ??= 10;
console.log(b); // "something"

‘with’ statement

The with statement lets you use objects as the scope for a block statement.

For example, take let’s say you want to log the ceil of 0.5, you can do:

console.log(Math.ceil(0.5)); // 1

With with, you can do:

with (Math) {
  console.log(ceil(0.5)); // 1
}

// Out of 'with (Math) {...}', it returns an error
console.log(ceil(0.5)); // Uncaught ReferenceError: ceil is not defined

Note: The use of with has been discouraged for having a couple of caveats and edge cases. You can read more about it on MDN.

function ‘arguments’ array

You always have access to the arguments Array-like object inside a function, even if no parameters are defined:

function sum() {
  let result = 0;
  for (const argument of arguments) {
    result += argument;
  }
  return result;
}

sum(5, 8); // 13

Functions can have properties

Functions in JavaScript are objects and therefore they can have properties attached to them, like so:

function sum() {
  sum.a ??= 10;
  sum.b ??= 20;

  return sum.a + sum.b;
}

console.log(sum()); // 30

sum.a = 30;
console.log(sum()); // 50

Warning: This is simply for the sake of example, normally you would be passing values as parameters instead.

Note: Check “Nullish coalescing operator” above to understand what “??=” does.

WeakSet / WeakMap

A WeakSet/WeakMap is a Set/Map object with 2 main differences:

  • They can only contain objects
  • WeakSet/WeakMap holds only references to the objects (hence “weak”). This means that if the referenced object is garbage collected, the entry is deleted.

In essence, a WeakSet/WeakMap allows a developer to keep track of additional info related to a family of objects without adding properties to them directly.

For example, let’s say you want to keep track of how many clicks are made to buttons on a page. However, these buttons may be added and removed dynamically.

Using a Map or Set, you will have to sync the nodes as they are changed. However, using a WeakMap/WeakSet, once the nodes are garbage collected, the entries from WeakMap/WeakSet are removed automatically (no need for syncing on your part = more robust code).

Note: Check out this cool article for more info on WeakMaps


That’s everything for now. If you’ve enjoyed this article, share the love with your friends and colleagues!

Tags:

Tips

Share Article

Other Articles

brown rope on black background
Previous

The Crash Course on Node Redis

Next

5 JS Design Patterns Every Developer Should Know

Next
March 10, 2023

5 JS Design Patterns Every Developer Should Know

Previews
March 3, 2023

The Crash Course on Node Redis

brown rope on black background

No Comment! Be the first one.

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Subscribe
    Stay Updated!
    Get notified when awesome content is posted. Learn more everyday.
    Most Read
    Software Architecture
    5 JS Design Patterns Every Developer Should Know
    March 10, 2023
    5 Min Read
    yellow line on gray asphalt road
    Next.JS
    5 Next.JS Tips You Never Heard About
    March 27, 2023
    4 Min Read
    blue UTP cord
    Uncategorized
    Setting up a TURN server with Node: Production-ready
    May 19, 2023
    5 Min Read
    Most Engagement
    JavaScript
    Simple Backgrounds For Websites — Cheat Sheet

    This cheat sheet will help you the next time you're choosing a background for your website....

    February 21, 2023
    3 Min Read
    Most Recent
    grayscale photo of person holding glass
    JavaScript
    Top 10 JavaScript Coding Challenges You Must Absolutely Know
    May 17, 2023
    9 Min Read
    a person pointing at a rock with writing on it
    JavaScript
    7 Best Coding Challenge Websites for 2023: Improve Your Skills and Boost Your Career
    May 20, 2023
    5 Min Read
    blue UTP cord
    Uncategorized
    Setting up a TURN server with Node: Production-ready
    May 19, 2023
    5 Min Read

    Related Posts

    grayscale photo of person holding glass
    JavaScript
    Top 10 JavaScript Coding Challenges You Must Absolutely Know
    May 17, 2023
    9 Min Read
    Read More
    JavaScript
    The Ultimate Sith Lord Developer Guide
    April 28, 2023
    5 Min Read
    Read More
    four handheld tools on board
    React
    Tools to Speed Up React Component Creation
    May 5, 2023
    3 Min Read
    Read More
    Software Architecture
    5 JS Design Patterns Every Developer Should Know
    March 10, 2023
    5 Min Read
    Read More

    Helder Esteves

    Read everything there is to know about the JavaScript development ecosystem, from programming to project management.

    Recent

    7 Best Coding Challenge Websites for 2023: Improve Your Skills and Boost Your Career
    May 20, 2023
    Setting up a TURN server with Node: Production-ready
    May 19, 2023

    Categories

    JavaScript9
    Uncategorized4
    Software Architecture2
    React2

    Contact

    Reach out to us through helder.writer@gmail.com

    © 2022, All Rights Reserved.

    Go to mobile version