JavaScript Introducing ES2015 Objects and New Collection Types Set

Sergi Oca
Sergi Oca
7,981 Points

Is this an Object or a normal variable...?

I feel like this course is either going to fast or it just needs some more basics covered before it. But I'm just confused looking at this:

let stevenJ = {name: "Steven", age: 22}, sarah = {name: "Sarah", age: 25};

What is that? Is it new from ES6? Is it an object? A variable holding keys and values...? I'm feeling a bit dumb at the moment because I don't recall ever seeing that before.

Would this be the same as:

let stevenJ = {name: "Steven", age: 22};

let sarah = {name: "Sarah", age: 25};

?

Umesh Ravji
Umesh Ravji
42,348 Points

It would give you the same result :)

3 Answers

Umesh Ravji
Umesh Ravji
42,348 Points

Hi Sergi,

let stevenJ = {
    name: "Steven",
    age: 22
}, sarah = {
    name: "Sarah",
    age: 25
};

Those would be objects (created using object literals), which are essentially name/value pairs. The let keyword is new to es6, there's a workshop on let and const here: https://teamtreehouse.com/library/defining-variables-with-let-and-const

Sergi Oca
Sergi Oca
7,981 Points

Thank you for your answers.

Dotun Ogunsakin
Dotun Ogunsakin
13,475 Points

It's been 2 months since the original question, and Sergi has probably come to a better understanding now, but I will post a few details that may help him or anyone else:

What happened in that single line was a (shorthand) multiple assignment in one let/var/const declaration. This is not new to ES6, and not even unique to JavaScript (pretty much all C-based languages do this - and possibly other languages too).

var a = 3, b = 7;
//a is 3
//b is 7
//NOTE: with const, a & b will be constants

As Umesh stated, it is no different than declaring them line-by-line. And there is an even more interesting variant (though, can be considered bad form):

var a = b = c = 3 + 4;
//a, b, and c will be 7!
//NOTE: with const, only the first will be a constant!

This is because values are assigned right-to-left of the assignment operator (again, in C-based languages, and possibly others). So, 3 + 4 first gets evaluated, then 7 is assigned to c, then c (now 7) is assigned to b, then b (also now 7) is assigned to a.

Joshua Worley
Joshua Worley
14,691 Points

Hey Sergi,

Basically, it's shorthand for declaring more than one variable. So your guess was right.

let stevenJ = {name: "Steven", age: 22}, sarah = {name: "Sarah", age: 25};

is the same as

let stevenJ = {name: "Steven", age: 22};
let sarah = {name: "Sarah", age: 25};

The course does go pretty fast, because there's a ton to cover. But in general I'd recommend taking the time to watch the vids, then trying the code your self, then contemplating it a bit, figuring out the things you don't know, looking it up, then creating your own examples. As someone who works in frontend IT, it's quite clear that the whole point of learning this stuff is so that you'll one day become a wizard and be able to create programs mostly from your own knowledge anyways. Practice practice practice