4 Powerful JavaScript Operators You’ve Never Heard Of

Tram Ho

Let’s learn some new JS operators!

Have you ever spent a lot of time reading through the Mozilla docs? If so, you know that there is a lot of information about JavaScript out there. This makes it easy to ignore the regular JavaScript operators.

However, just because these operators are uncommon doesn’t mean they are not strong! Each of them looks syntactically similar, but remember to read about each as they work in different ways.

Let’s find out together!

1. Operator ??

In JavaScript, the ?? operator is called the nullish binding operator . This operator will return the first argument if it is not null / undefined , otherwise it will return the second argument. Let’s see an example.

When assigning default values ​​to a variable , JavaScript developers often rely on the logical OR operator , like so.

Above I have created a moneyAmount function that returns the current balance of the user. The || operator Used to identify users without an account. However, what does that mean when a user doesn’t have an account? It would be more accurate to consider no account as null instead of 0 because bank accounts can exist without money (or negative). In the above example, the || operator see 0 as an error value and therefore do not register that our users have a $ 0 account. Fix this by using the nullish combination operator.

To summarize, operator ?? allows us to specify default values ​​while ignoring dummy values ​​like 0 and empty strings.

2. Operator ?? =

?? = is also known as the nullish logical assignment operator and is closely related to what we have learned before. Let’s see how they come together.

This assignment operator will only assign a new value if the current value is null or unknown. The example above highlights how this operator is essentially a syntax line for assigning nullish. Next, let’s see how this operator differs from the default parameters.

3. Operator?.

Optional string operator?. Allows developers to read the values ​​of properties deeply nested in a sequence of objects without having to explicitly validate each reference during execution. When a reference is empty, expression stops evaluating and returns an undefined value. Let’s see an example.

See the next example.

The example above combines all of the operators we have learned so far. Now we have created a function to add the plan. I have also used the nullish operators to provide default values. This function will accept false values ​​like ‘0’ as a valid parameter. Which means our budget can be set to 0 without any errors.

4. Operators?

The ternary operator? Accepts three operands: a condition, an expression to execute if the condition is true, and an expression to execute if the condition fails. See the following example:

If you’ve taken the time to learn JavaScript, you may have seen ternary operators before. However, did you know that ternary operators can be used to assign variables?

We can even use it to assign nullish values.

See the next example:

Before we finish, use the ternary operator to refactor the function from the previous example.

5. References

  1. https://medium.com/javascript-in-plain-english/4-powerful-javascript-operators-youve-never-heard-of-487df37114ad
  2. https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Expressions_and_Operators
Share the news now

Source : Viblo