Use cases for parentheses, braces, brackets and quotes JavaScript in language
parentheses()
Use cases
- Provide arguments for function declaration, e.g.:
function doSomething (how, when) {}
- Execute function, e.g.:
var result = doSomething()
- Execute function with parameters, e.g.:
doSomething(nice)
- Provide conditional statements conditions, for example loop, if, and switch statements, e.g.:
while (var < 0) {do something}
- Provide switch statement conditions, e.g.:
switch (bestFoods) {
case "pizza":
console.log("get it");
case ...
- Execute Immediately Invoked Function Expressions (IIFE), e.g.:
(function () {
var shout = "I'm alive!";
alert (shout);
}) ();
brackets[]
Use cases
-
Creating arrays, e.g.:
myArray = [element1, element2, element3]
-
Accessing array elements by their index, e.g.
myArray = ["cat", "dog", "mouse"]
console.log(myArray[0]);
-
Accessing and updating properties of objects, especially if the property name has a space or different not-allowed sign in the name, e.g.
cat.enemies["too-much water"]
myObject["some name"]="newName"
braces{}
Use cases
-
Creating objects, e.g.:
var objectCat = {
"name": "Cat",
"legs": 4,
"enemies": ["dogs", "water"]}
-
Creating conditional statements, e.g.:
if (conditions) {statement body};
-
Initialising functions, e.g.:
function myFunction () {function body};
single quotes ''
Use cases
-
Accessing object properties with bracket notation, e.g.
objectCat['name']
-
String literals, e.g.
var shout = 'I'm alive'
double quotes ""
Use cases
-
Same as single quotes, plus:
-
Use to create objects with string elements, e.g.:
var myObjectDinner = {"soft drink", "salad", "fish"}
(JSON strings must be double quoted source)
-
Single and double quotes next to each other are not allowed in JS, hence the following code:
'Quoth the raven:\n\t\"Nevermore!\"'
to produce the following string:
Quoth the raven:
      "Nevermore!"
↑