Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curly braces inside JavaScript arguments for functions

What do the curly braces surrounding JavaScript arguments for functions do?

var port = chrome.extension.connect({name: "testing"});
port.postMessage({found: (count != undefined)});
like image 951
milan Avatar asked Nov 10 '10 17:11

milan


People also ask

What is written inside the curly brackets on a function in JavaScript?

It's an object literal. Show activity on this post. Basically the curly braces {} are the another way for creating objects in javascript. This is equivalent to the "new Object()" syntax.

Are curly braces required around the statements in a function?

Yes, it works, but only up to a single line just after an 'if' or 'else' statement. If multiple lines are required to be used then curly braces are necessary.

Can you use brackets in JavaScript?

To edit JavaScript code, you can use the same techniques that you use to edit HTML or CSS. However, a JavaScript file must have the . js extension. By default, when you type the left brace, left parenthesis, or left quotation mark in a JavaScript statement, Brackets adds the right brace, parenthesis, or quotation mark.

Are braces necessary in one line statements in JavaScript?

No, curly braces are not necessary, However, one very important reason to use the curly brace syntax is that, without it, there are several debuggers that will not stop on the line inside the if statement.


3 Answers

A second possible answer has arisen since this question was asked. Javascript ES6 introduced Destructuring Assignment.

var x = function({ foo }) {    console.log(foo) }  var y = {   bar: "hello",   foo: "Good bye" }  x(y)   Result: "Good bye" 
like image 180
Matthias Winkelmann Avatar answered Sep 26 '22 01:09

Matthias Winkelmann


The curly braces denote an object literal. It is a way of sending key/value pairs of data.

So this:

var obj = {name: "testing"}; 

Is used like this to access the data.

obj.name; // gives you "testing" 

You can give the object several comma separated key/value pairs, as long as the keys are unique.

var obj = {name: "testing",            another: "some other value",            "a-key": "needed quotes because of the hyphen"           }; 

You can also use square brackets to access the properties of the object.

This would be required in the case of the "a-key".

obj["a-key"] // gives you "needed quotes because of the hyphen" 

Using the square brackets, you can access a value using a property name stored in a variable.

var some_variable = "name";  obj[ some_variable ] // gives you "testing" 
like image 35
user113716 Avatar answered Sep 22 '22 01:09

user113716


Curly braces in javascript are used as shorthand to create objects. For example:

// Create an object with a key "name" initialized to the value "testing"
var test = { name : "testing" };
alert(test.name); // alerts "testing"

Check out Douglas Crockford's JavaScript Survey for more detail.

like image 33
camomilk Avatar answered Sep 23 '22 01:09

camomilk