Aashman Verma
Aashman Verma's Blog

Follow

Aashman Verma's Blog

Follow
Logical assignment, For of loop & Optional chaining

Logical assignment, For of loop & Optional chaining

Aashman Verma's photo
Aashman Verma
ยทFeb 7, 2023ยท

3 min read

Play this article

Logical assignment

The logical assignment operator in JavaScript is &&= and ||= . Let me explain it through the code below.

let x = 0;
let y = 1;
console.log(x&&=y); // executes as x && (x=y), returns 0
console.log(x||=y); // executes as x || (x=y), returns 1

The operator x&&=y executes && operator between x and (x=y).

The operator x||=y executes the || operator between x and (x=y).

Here we are using the "let" keyword to declare a variable because of the execution of expression x=y which can't be executed in case the variable is declared with the const keyword. If we declare the variable with the const keyword an error will occur.


For of loop

For of loop, loops over an iterable element (i.e element that can be looped over), for example - Arrays, String, Maps, Sets. Objects are not iterable elements by nature but we can define their iteration behavior with the "Symbol.iterator" property.

In the case of arrays

// declaring an array
const array = [1,2,3,4,5,6,7,8,9,10];
for (const numbers of array) {
    console.log(numbers);
}

Here "numbers" loops over each element of the array as it is iterable, after each loop over the element we log it to the console. The output will be all the elements of the array.

In the case of sets

Sets are like arrays that take input of all elements of an array, remove the duplicate elements in it and store them inside curly braces {} instead inside [] in the case of arrays.

// declaring a set 
const array = new Set([1,2,3,3,4,2]);
console.log(array); // returns {1,2,3,4}
for (const numbers of array) {
    console.log(numbers); 
    // output will be all elements of the array
}

In the case of a map

Maps are iterable, the difference between an object and a Map in JavaScript is that objects have properties with string keys, while Map objects have properties with keys of any type.

// declaring map
const map = new Map([
  [1, 'one'],
  [2, 'two'],
  [3, 'three']
]);

for (const value of map.values()) {
// .values() function helps us getting values inside map
// similarly we can use .keys() function for keys of map
  console.log(value);
}
// output is - 
// one
// two 
// three

Optional chaining

It is a feature that allows you to access the properties of an object only if they exist. It can be useful in situations where you need to access properties of nested objects, but you're not sure if those properties exist.

const obj = {
  a: {
    b: {
      c: 42
    }
  }
};

const data = obj?.a?.b?.c;
console.log(data); // 42

The value of the data variable executes as if the obj variable exists then go for searching inside it if a exist then go for searching b inside it if b exists then go for return c inside it to the variable data.

If the condition becomes false then it will return the undefined value.


In case you find any of the above topics difficult to understand, feel free to comment below if you want blog on a specific topic.


Follow up -

In case you find my work good and interesting follow me on hashnode Aashman Verma

as well as on my Twitter handle in my bio. ๐Ÿ˜€

Kindly comment below, it will help me to improve and share more about javascript in an easy language. In case you are still confused you can comment below or dm me on Twitter :). You can also suggest topics for the blog.

ย 
Share this