operador ternário javascript w3schools code example

Example 1: expresiones ternarias javascript

var condition = false;
var result1 = "First result";
var result2 = "Second result";
var result = condition ? result1 : result2;
//result1 will be assigned to result if condition is true.
//If not, result2 will be assigned

Example 2: less than equal to in javascript

| <= | less than or equal to |	x <= 8 | true |

Example 3: javascript ... operator

//The operator ... is part of the array destructuring.
//It's used to extract info from arrays to single variables.
//The operator ... means "the rest of the array".
var [head, ...tail] = ["Hello", "I" , "am", "Sarah"];
console.log(head);//"Hello"
console.log(tail);//["I", "am", "Sarah"]

//It can be used to pass an array as a list of function arguments
let a = [2,3,4];
Math.max(a) //--> NaN
Math.max(...a) //--> 4

Tags:

Cpp Example