Parentheses indicate to the compiler that you want to evaluate expressions inside them. The compiler will execute from the innermost to the outermost. It is the same as you would see in maths. So
(4 + (2 * (6 - 3)))
// - is an infix function with arguments 6 and 3, apply that
(4 + (2 * 3))
// * is an infix function with arguments 2 and 3, apply that
(4 + 6)
// + is an infix function with arguments 4 and 6, apply that
10
When the parentheses come directly after an identifier (a variable name), the compiler will assume the identifier refers to a function, so again, it will evaluate what is in the parentheses to values, then attempt to apply the resulting values to a function.
A function in Javascript is a prefixed expression. +
or -
are infix. With a defined function in JS you put the identifier before your values/expressions, which go in the parentheses. Some languages let you define infix functions or postfix functions; JS does not.
You define what the [prefix] expression should do by using the function
keyword, this denotes that it is a description of how the compiler should convert the expression to a value:
function add(a,b) { return a + b }
Then you use it:
add(4, (2 * (6 - 3)))
add(4, (2 * 3))
add(4, 6)
10
Curly brackets are to denote blocks in JavaScript, eg a collection of related bits of code that should be evaluated together. They are also used to denote objects, the mechanism is the same:
{
// These things are scoped inside this block:
const foo = 'bar';
const baz = 1;
}
// This is an object, note the `:` and the commas at the end of the lines:
{
foo: 'bar',
baz: 1,
}
So in the example, you’re using a keyword for
, which is the special syntax used to define a for loop in JS. It isn’t quite the same as a function - the for loop itself isn’t an expression, so doesn’t quite work the same way as a normal function/expression would (it doesn’t evaluate to a value, it just runs some code over and over). They work in very much the same way: the expression in the parentheses is evaluated, then the code inside the block ({
, }
) is run. The for
tells the compiler to go back and evaluate the expression again then run the code in the block until a condition is met:
// for (value to initialise loop; end condition; expression to evaluate on each loop)
for (var i = 1; i < 6; i++) { myArray.push(i); }
// i is 1, so i is less than 6. Push 1 to myArray. Loop - i++ means i goes up by one
(i = 2; i < 6; i++) { myArray.push(i); }
// i is 2, so i is less than 6. Push 2 to myArray. Loop - i++ means i goes up by one
(i = 3; i < 6; i++) { myArray.push(i); }
// i is 3, so i is less than 6. Push 3 to myArray. Loop - i++ means i goes up by one
(i = 4; i < 6; i++) { myArray.push(i); }
// i is 4, so i is less than 6. Push 4 to myArray. Loop - i++ means i goes up by one
(i = 5; i < 6; i++) { myArray.push(i); }
// i is 5, so i is less than 5. Push 5 to myArray. Loop - i++ means i goes up by one
(i = 6; i < 6; i++) { myArray.push(i); }
// i is 6, so i is NOT less than 6. Stop looping
note
expression: a collection of values/variables/functions/etc that the computer can evaluate to a value.
identifier: a name that identifieds a unique object, in JS, a variable