The Complete JS Notes #7 🧵

Baris Balli
Dev Genius
Published in
3 min readJul 15, 2022

--

Welcome to the next article of my JavaScript notes.

In this article we will talk about: Rest Pattern and Parameters, Short Circuiting and Nullish Coalescing Operator

All the content is taken from Jonas Schmedtmann’s amazing course The Complete JavaScript Course 2022: From Zero to Expert!

Please purchase the course to understand the content, this is simply my summarized notes of the course.

//Spread operator because on the RIGHT side of the assignment operator
const arr = [1, 2, …[3, 4]];//REST because on the LEFT side of the operator
const [a, b, …others] = [1, 2, 3, 4, 5];

Rest operator is called rest because it is the rest of an array for exp

Rest operator always must be used at the end of the collection, otherwise js wouldn’t know when to end

const [pizza, , risotto, …others, bread] = […restaurant.mainMenu, …restaurant.starterMenu];// It returns an error rest must be the last element

Because of this reason, there can only be 1 rest operator per collection.

Just like spread operator, we can use rest in objects

const { saturday, …weekdays } = restaurant.workingDays;

Use rest as parameters

const manyParameters = (…nums) => { //Rest operator
cl(nums)
}manyParameters(2,3,4);manyParameters(5,6,23,555,12,43);parameters = [5,6,23,555,12,43];manyParameters(…parameters); //Spread operator

These return an array of parameters

We can specify few arguments before using rest

const degree = (centigrad, …others) => {…}degree(true, 27, 0.86);degree(false, 112, 0.90);

Short circuiting && , ||

There are 3 things to know about logical operators

  • They can use ANY data type
  • They can return ANY data type
  • They can use short-circuiting

short circuiting for OR operator is:

if the first value is a truthy value Js wouldn’t even look to the second operator it returns the first one immediately

cl(3 || ‘Jonas’); // Prints 3cl( ‘’ || ‘Jonas’ ) // Jonascl( true || 0 ) // truecl( undefined || null ) // null

In the last one, both of them are falsy but it returns the second.

Returns ‘Hello’

Here both of them does the same job. So short-circuiting or is just like terniary operation.

And operator is the opposite of the or

cl( 0 && ‘Jonas’ ) // 0cl ( 7 && ‘Jonas’ ) // Jonas

Now a pracctical example

// Practical Exampleif(restaurant.orderPizza) {restaurant.orderPizza(spinach, mushroom)}restaurant.orderPizza && restaurant.orderPizza(spinach, mushroom) // Best practice

You shouldn’t change all of your if statements with short-circuits because it would make your code very hard to read.

The nullish coalescing operator

In this example even though we have 0 guests it will be false and give 10 to the console

const guestCorrect = restaurant.numGuests ?? 10;cl(guestCorrect);

This operator just looks for the nullish values such as (undefined and null) otherwise it accepts the value as true.

Follow me on twitter 💣🔥

Read me and many other great tech bloggers in Dev Genius

--

--