About JavaScript Logical Assignments

About JavaScript Logical Assignments

ยท

4 min read

Introduction

In this article, we are going to explore JavaScript logical assignment operators which were introduced in ES2021. and also at the end of this article, I am going to share information about the App which is going to help us for staying up-to-date with the latest coding and programming-related news.

ES2021 Introduces the 3 useful JavaScript logical assignment operators including:

  1. Logical OR assignment operator ( ||= )

  2. Logical AND assignment operator ( &&= )

  3. Nullish coalescing assignment operator ( ??= )

The logical OR assignment operator ( ||= )

The logical OR assignment operator will accept the two operands and assigns the right-side operand to the left side only if the left-side operand is falsy.

Syntax:

a ||= b

In this syntax ||= operator only assigns the value of b to the a, if a is falsy.

For example:

let name;
name ||= '<ANONYMOUS>';
console.log(name); // Expected Output:- <ANONYMOUS>

Explanation

In this example, the variable name is undefined and as we know that undefined is a falsy value. since the variable name is undefined the operator ||=
assigns the '<ANONYMOUS>' to the name variable. and the output will be '<ANONYMOUS>' as expected.

Example 2:

const name = 'John Doe';
name ||= '<ANONYMOUS>';
console.log(name) // Expected output:- John Doe

Explanation

Now, in this example variable name is a 'John Doe'. so it's truthy, therefore logical OR assignment operator does not assign the string '<ANONYMOUS>' to the name.

The logical AND assignment operator ( &&= )

The logical AND assignment operator assigns the right side operand to the left side operand only if the value of the left side is truthy.

Syntax:

a &&= b

In this syntax &&= operator assigns the value of b to the a, only if the value of a is truthy.

For Example:

const userInfo = {
  firstName: "Tony",
  lastName: "Stark",
};

userInfo.lastName &&= "Shark";
console.log(userInfo); 
// output:- { firstName: 'Tony', lastName: 'Shark' }

Explanation

In this example, the &&= operator is used to change the last name of the userInfo object as the last name is truthy.

The nullish coalescing operator ( ??= )

The nullish coalescing operator is also known as the logical nullish assignment operator. Will assign the right side operand to the left side operand only if the left side operand is null or undefined.

syntax:

a ??= b

In this syntax the ??= operator assigns the value of b to the a, only if the value of a is null or undefined.

For Example:

const user = {
  userName: "Jane",
};
user.nickName ??= "anonymous";

console.log(user); 
// Output: { userName: 'Jane', nickName: 'anonymous' }

Explanation

In this example user.nickname is undefined, therefore it's nullish. Therefore ??= operator assigns the string 'anonymous' to the user.nickName property.

And here comes the interesting part of this article. In this, I am going to tell you about the app named DevByets, where we can get coding and other tech-related news and information.

DevBytes App

DevBytes provides coding news in a short and crisp format. This short news app has been designed especially for developers to stay updated with the latest happenings in the tech world. It selects the latest advanced coding and technology news from both national and international sources, famous tech blogs, and social media and summarizes them in just 64 words.

we'll have access to the most thorough and up-to-date news coverage from the tech industry with DevBytes. DevBytes has you covered for the most recent product releases from Apple, Google, or Microsoft as well as news on fascinating startups.

This app also provides code snippets that we can copy and can run in our favorite IDE.

It is a platform that provides material to all coders, whether experts or beginners. An amazing must-have application. This application may be used as a low-code platform, a tool for optimizing code, or a music player. Get all the information we need in one location.

DevBytes has received good reviews from both users and IT experts, earning 4.6 stars on Google Play.

Download this app by following the link given below ๐Ÿ‘‡

DevBytes

Did you find this article valuable?

Support Charukirti Chougule by becoming a sponsor. Any amount is appreciated!

ย