Using {} in JavaScript?

A very simple question, but I am completely new to JS and it is not easy to find.

If I see a line like this:

var colours = {}, r, g, b; 

I get that this is just an ad for 3 vars, but what does the bit {} mean? I donโ€™t see anywhere, which gives me an idea that this announcement?

Thanks!

+4
source share
7 answers
  var colours = {}, r, g, b; 

This declares 4 variables that match

  var colors = {}; // this is an empty object like, var colors = new Object(); var r; // undefined var g; // undefined var b; // undefined 
+3
source

Declares a new object and the equivalent of new Object();

+5
source

This means that the colors will be an object.

+2
source

{} declares an object without members. Like an empty data container. [] will declare an empty array.

Arrays have numerical indices (and some convenient methods), and objects can have string indices (but they don't have array methods)

+2
source

This is a declaration of four variables, not three. One of them is called colours and is initialized {} . The rest are called r , g and b , and their initial values โ€‹โ€‹are undefined . {} is an empty object literal.

+2
source

It declares an empty object literal.

+1
source

Initializes colors new, empty object.

Although objects in JavaScript can have methods, they are also often used as associative arrays, which is quite likely in this case (assumption based on the name and that it was initialized without any properties).

+1
source

Source: https://habr.com/ru/post/1411976/


All Articles