1let obj1 = { foo: 'bar', x: 42 };
2let obj2 = { foo: 'baz', y: 13 };
3
4let clonedObj = { ...obj1 };
5// Object { foo: "bar", x: 42 }
6
7let mergedObj = { ...obj1, ...obj2 };
8// Object { foo: "baz", x: 42, y: 13 }
1let a = { a: 0, b: 1 };
2let b = { b: 2, c: 3 };
3let c = { ...a, ...b };
4
5console.log(c);
6
7output -> {a: 0, b: 2, c: 3}
1function sum(x, y, z) {
2 return x + y + z;
3}
4
5const numbers = [1, 2, 3];
6
7console.log(sum(...numbers));
8// expected output: 6
9
10console.log(sum.apply(null, numbers));
11// expected output: 6
12
13/* Spread syntax (...) allows an iterable such as an array expression or string
14to be expanded in places where zero or more arguments (for function calls)
15or elements (for array literals) are expected, or an object expression to be
16expanded in places where zero or more key-value pairs (for object literals)
17are expected. */
18
19// ... can also be used in place of `arguments`
20// For example, this function will add up all the arguments you give to it
21function sum(...numbers) {
22 let sum = 0;
23 for (const number of numbers)
24 sum += number;
25 return sum;
26}
27
28console.log(sum(1, 2, 3, 4, 5));
29// Expected output: 15
30
31// They can also be used together, but the ... must be at the end
32console.log(sum(...[4, 5, ...numbers]));
33// Expected output: 15
34
1/*
2JavaScript includes operators as in other languages. An operator performs
3some operation on single or multiple operands (data value) and produces a
4result. For example 1 + 2, where + sign is an operator and 1 is left operand
5and 2 is right operand. + operator adds two numeric values and produces a
6result which is 3 in this case.
7*/
8
1/* JavaScript shorthand -=
2-= is shorthand to subtract something from a
3variable and store the result as that same variable.
4*/
5
6// The standard syntax:
7var myVar = 5;
8console.log(myVar) // 5
9var myVar = myVar - 3;
10console.log(myVar) // 2
11
12// The shorthand:
13var myVar = 5;
14console.log(myVar) // 5
15var myVar -= 3;
16console.log(myVar) // 2
17