Skip to content
0:00 / 0:00

#javascript#commaoperator

I ❤ JavaScript: Mysterious Comma operator

Styled JavaScript

1.7K subscribers • 7 videos on Hallway

YouTube
  • Recommended by 2 people: 2 in engineering communities.

Intermediate. Assumes working knowledge.

987 views8 years ago2 recommendations

Programming languagesJavaScript

An explanation of how the comma operator works in JavaScript and how it behaves inside array brackets.

Hi let's talk today about a Comma operator and array. I'm Anatol and you're watching the Good Parts of the FrontEnd development. 1) ecma-international.org/ecma-262/5.1/#sec-15.4 According to ECMAScript Specification an Array in JavaScript has only one dimension. An array contains elements, these may be specified as primitives or data structures, for instance, an element can be an array. Therefore, this one [3,[2,1]] is a valid expression. Hereby we’ve solved the first part of the puzzle. Let’s go back to our initial sample. [x,[a,b],z][1] = [a,b] - this means that we take the element No=1 of the array. Then one more time, take the element No=0: [a,b][0] = [a] and get [a]. 2) ecma-international.org/ecma-262/5.1/#sec-11.14 Let’s read the second part of initial expression: [3,2,1]. According to ECMAScript Specification a Comma operator in JavaScript evaluates an expression before comma and returns the very right assignment after the last comma. Therefore, this expression `3,2,1` returns a number `1`. For example, this code snippet if (z == 1) { a = 2 a1 = 3 } could be simplified (or "complicated", as you wish) to the following code: a1 = z === 1 && (a = 2, 3) Assign value = 2 to the variable "a", then assign value 3 to the variable "a1". Hence, the initial sample [3,[2,1],0][3,2,1][2,1,0] is evaluated as: First step: `[3,[2,1],0][3,2,1]` returns the 1st element, an array [2,1] Second step: `[2,1][2,1,0]` returns 0th element, the number 2 We came to the same result - 2. That's it. So easy. If you like this video give it "thumbs up". Thanks for watching and stay curious. Used: looperman.com: Rosita Lofi Beat by Fanto8BC, Progress by nightingale, Chiptune Melody by L0neW0lf, Film Noir by LankFrampard, Russian Boogie by nightingale

Why it's here

2 people recommend this

u/r-wabbitin r/node • Recommends

I ❤ JavaScript: Mysterious Comma operator

u/katerina-ser60in r/Frontend • Recommends

I ❤ JavaScript: Mysterious Comma operator