three dot operator javascript code example
Example 1: javascript ... operator three dots
function sum(x, y, z) {
return x + y + z;
}
const numbers = [1, 2, 3];
console.log(sum(...numbers));
console.log(sum.apply(null, numbers));
function sum(...numbers) {
let sum = 0;
for (const number of numbers)
sum += number;
return sum;
}
console.log(sum(1, 2, 3, 4, 5));
console.log(sum(4, 5, ...numbers));
Example 2: dot operator javascript
Lets first look at Dot notation. Consider this example below:
let obj = {
cat: 'meow',
dog: 'woof'
};
let sound = obj.cat;
console.log(sound);
I want to bring your attention to the fifth line where we’re using dot notation: let sound = obj.cat;. This is an example of dot notation. You can access properties on an object by specifying the name of the object, followed by a dot (period) followed by the property name. This is the syntax: objectName.propertyName;.
When working with dot notation, property identifies can only be alphanumeric (and _ and $). Properties can’t start with a number.