DEV Community

Victor Chan
Victor Chan

Posted on

3 Javascript features you probably didn't know

  1. Deep object destructuring You probably know you can destructure objects, but did you know you can destructure a destructured object?
const {
    dog,
    cat: { legs, eyes, breed },
  } = pets;
Enter fullscreen mode Exit fullscreen mode
  1. Destructuring an array You can also destructure an array by its index
const {0: first, 5: sixth} = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"];
console.log(first) // expected output: "Jan"
console.log(sixth) // expected output: "Jun"
Enter fullscreen mode Exit fullscreen mode
  1. The comma operator (,) Evaluates each of its operands (from left to right) and returns the value of the last operand.
let x = 1;

x = (x++, x);

console.log(x); // expected output: 2

x = (2, 3);

console.log(x); // expected output: 3
Enter fullscreen mode Exit fullscreen mode

This is used when you need multiple variables for for loops

for (var i = 0, j = 9; i <= 9; i++, j--)
  console.log('a[' + i + '][' + j + '] = ' + a[i][j]);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)