What Do Curly Braces in JavaScript Mean

What do curly braces in JavaScript mean?

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.

When should I use curly brackets in javascript? In general

Curly brackets are used to denote a block of code. These are generally used while writing control flow statements, loops, functions etc.

For example:

for ( var index = 0; index < 10; index++ ) {
//this will contain some lines of code
}

function doSomething() {
//this will have a set of statements
}

if ( expression ) {
//some statements to execute if condition is true
} else {
//Statements to execute if condition is false
}

Javascript object literal notation also uses curly brackets.

Example:

var personObject = {firstName: "John", lastName: "Doe"};

Meaning of curly braces in array.map()

That’s the behavior of arrow function. Arrow function syntax is designed to be terse.

(arg) => [single expression]

If what follows the arrow is one single expression without curly braces, it implies returning the value of that expression. The return keyword is omitted.

If wrapping curly braces follows the arrow, then what goes inside the braces are treated as normal function body, and return keyword must be used if you mean to return a value.

So (x) => x + 1 is translated to:

function (x) {
return x + 1;
}

And (x) => { x + 1 } is translated to:

function (x) {
x + 1;
}

I must also add that (x) => ({ x }) is translated to:

function (x) {
return { x: x };
}

Curly brace {...} can mean an object literal, a function body or a code block.

In order to disambiguate, you need to put parenthesis around the braces to tell the parser to interpret {...} as an “object literal expression”.

What do curly braces around a variable in a function parameter mean

This is destructuring assignment syntax.

As another example, the following two lines of code are equal:

const { items } = args

const items = args.items

Simply put, it is a simplified way of accessing specific field of a given variable for further use in that scope.

In your original example, it is declaring a variable items for use in the function body that is the items field of that first argument.

const SortableList = SortableContainer(({items}) => {
// do stuff with items here

is equal to

const SortableList = SortableContainer((input) => {
const items = input.items
// do stuff with items here

What does ${} (dollar sign and curly braces) mean in a string in JavaScript?

You're talking about template literals.

They allow for both multiline strings and string interpolation.

Multiline strings:

console.log(`foobar`);// foo// bar

What does a variable directly followed by empty curly braces mean in Javascript?

The {} represents an empty object.

map[event] = phi will add (or overwrite) a property on the map object with the name event and assign it to a value of phi. This way, you can do map.EVENT_NAME to get the value of phi for that event.

After performing storePhi("pizza", 0.069);, map will look like this:

console.log(map);
map = {
pizza: 0.069
}

console.log(map.pizza);
map.pizza = 0.069

What do {curly braces} around javascript variable name mean

This is what's known as a destructuring assignment, and it's a new feature of JavaScript 1.7 (and ECMAScript 6) (Currently, only available as part of the Firefox JavaScript engine.) Roughly, it would translate into this:

var ActionButton = require("sdk/ui/button/action").ActionButton;

It seems silly in this example, as there's only one item being assigned. However, you'd be able to use this pattern to assign multiple variables at once:

{x, y} = foo;

Is the equivalent to:

x = foo.x;
y = foo.y;

This can also be used for arrays. For example, you could easily swap two values without using a temporary variable:

var a = 1;
var b = 3;

[a, b] = [b, a];

Browser support can be tracked using kangax' ES6 compatibility table.



Related Topics



Leave a reply



Submit