What do curly braces in JavaScript mean?

JavascriptJqueryCss

Javascript Problem Overview


I found this in a jQuery file:

xxx.css({ 'float' : 'right' });

What do the curly braces do?

Javascript Solutions


Solution 1 - Javascript

In your case it is an object passed to your css function.

myObj={} // a blank object

Here you can use this too

myObj={'float' : 'right'}
xxx.css(myObj);

Here is another example of object

var myObj={
    'varOne':'One',
    'methodOne':function(){ alert('methodOne has been called!')}        
}
myObj.methodOne();​ // It will alert 'methodOne has been called!'

A fiddle is here.

Solution 2 - Javascript

The curly braces in the code you've shown define an object literal

Solution 3 - Javascript

In javascript curly braces are used for several purposes.

I your case these are used to create a key-value pair.

In others cases curly braces are used to combine a set of statements in a block. And sometimes they are used to create objects like var abc = { "a": 1, "b": 2 };

Solution 4 - Javascript

This is the top search engine result for "javascript braces". As such it's worth mentioning that braces in JavaScript can be used for:

Solution 5 - Javascript

It's an object literal.

var x = {'float': 'right'} is the nicer/shorter form of var x = new Object(); x.float = 'right';

Solution 6 - Javascript

Basically the curly braces {} are the another way for creating objects in javascript. This is equivalent to the "new Object()" syntax.

Solution 7 - Javascript

curly braces identify an Object like so:

timObject = {
	property1 : "Hello",
	property2 : "MmmMMm",
	property3 : ["mmm", 2, 3, 6, "kkk"],
	method1 : function(){alert("Method had been called" + this.property1)}
};

in jQuery they are used to provide an Object with options for your method. You could also write your code like so xxx.css("width","10px").css("font-size","30px"); But passing it an Object makes it faster and more readable

xxx.css({"width":"10px","font-size":"20px"});

Solution 8 - Javascript

Creates an object.

var myObject = {"element" : "value"};
alert(myObject.element); // Would alert: "value"

Solution 9 - Javascript

That is an object literal

> An object literal is a list of zero or more pairs of property names and associated values of an object

Solution 10 - Javascript

They encapsualte the css attributes in this example.

Normally curly brackets represent a function or an encapsulated piece of code that needs to be executed as one.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMouse HelloView Question on Stackoverflow
Solution 1 - JavascriptThe AlphaView Answer on Stackoverflow
Solution 2 - JavascriptRafaelView Answer on Stackoverflow
Solution 3 - Javascriptme_digvijayView Answer on Stackoverflow
Solution 4 - JavascriptMarkus RView Answer on Stackoverflow
Solution 5 - JavascriptThiefMasterView Answer on Stackoverflow
Solution 6 - JavascriptclklachuView Answer on Stackoverflow
Solution 7 - Javascriptmas-designsView Answer on Stackoverflow
Solution 8 - JavascriptjchavannesView Answer on Stackoverflow
Solution 9 - JavascriptQuentinView Answer on Stackoverflow
Solution 10 - JavascriptbeaumondoView Answer on Stackoverflow