re: Understanding basics of array in javascript VIEW POST

VIEW PARENT COMMENT VIEW FULL DISCUSSION

I use let only when i want to modify variable's value.

In below example you can't use const because the value of variable is modified by increment one until the condition is false.

  for(let i = 1; i < 3; i++) {
    console.log(i); // 1 and 2
  }

In below example, the constant variable(random) hold each values of that array.

  for(const random of ['a', 'b', 1, 2]  ) {
    // Here it print values that held by constant random without modify it
    console.log(random); // a b 1 2
  }

Remember, to be constant does not mean to hold only single data. const can also hold list of data. Good example is array.

 // Here constant colors is holding two color
 const colors = ['purple', 'black'];

 // First color is purple
 console.log(colors[0]); // 'purple'

 // Second color is black
 console.log(colors[1]); // 'black'
code of conduct - report abuse