What is the difference?

Hi guys im a little confused about , and ; because it seems like that they make a difference, for example in loops if i write = (with , ) (in line 2)

const vacationSpots = ['Bali', 'Paris', 'Tulum'];
for (let i = 0, i < vacationSpots.length, i++ ) {
  console.log('I would love to visit ' + vacationSpots[i])
}

it gives this error = SyntaxError: Identifier 'i' has already been declared
but if i replace the , with the ; = (in line 2)

const vacationSpots = ['Bali', 'Paris', 'Tulum'];
for (let i = 0; i < vacationSpots.length; i++ ) {
  console.log('I would love to visit ' + vacationSpots[i])
}

i get the expected output which is =
I would love to visit Bali
I would love to visit Paris
I would love to visit Tulum


so i undrestand that they make a difference but what is the difference? (i hope you guys got/get the question)
and sometimes when i use the format code option to make the code cleaner it replace all the , with ; but why ?

1 Like

Because ; is the proper syntax for what you want to do and , isn’t. Programming syntax is very, very specific.

Note, a , in a for has its own meaning. It allows you to define extra actions within those ;. e.g.

const vacationSpots = ['Bali', 'Paris', 'Tulum'];
for (let i = 0, j=vacationSpots.length; i<j; i++) {
    console.log('I would love to visit ' + vacationSpots[i])
}

Also, if you’re iterating over an array and don’t care about the index, there are other syntax options. e.g.

const vacationSpots = ['Bali', 'Paris', 'Tulum'];
for (let x of vacationSpots) {
    console.log('I would love to visit ' + x)
}

or

const vacationSpots = ['Bali', 'Paris', 'Tulum'];
vacationSpots.forEach(x => console.log('I would love to visit ' + x));
2 Likes

so 3 things =
1 = Wow thanks for the explanation.
2 = The other methods you showed i wasnt familliar with but thanks for letting me know about those.
3 = So is there any thing that have a complete explanation about the syntaxes ? or i have to memorize to use which when.

1 Like

can i ask you another question?
what does the i++ do ?

in a simple loop i know whaat it does for example =

for (let counter = 0 ; counter < 4 ; counter++ ){ 
console.log(counter);
}

here i know that after each round the counter get a +1 so if its 0 it will become 1 and 2 and 3

but here we are doing it with the arrays and they dont get + anything ! so why we must write it ?

1 Like

So is there any thing that have a complete explanation about the syntaxes ?

A number of places. JavaScript actually has a standard. However, reading standards will rot your brain. I’d go with the Mozilla docs if you want to browse.

i have to memorize to use which when.

Don’t try to. Syntax is not exhaustive; you will pick it up the more you use a language. When you spot something you don’t know, look it up. It will have more meaning for you then and you’ll be more likely to remember it. Trying to memorize up front will only cause pain and suffering.

what does the i++ do ?

The ++ is an increment operator. This beast is from the days of C and has some quirky historical nuance that probably doesn’t entirely translate to JS. In practice, it’s usually identical to i += 1 or i = i + 1, but obviously shorter.

2 Likes

thanks , i even look online like you said and i found out

It increases the value of the variable with 1. In this case it makes sure the loop actually ends at some point, 
If you didn't increment the value, the loop would run forever

but i dont undrestand if increasing by 1 make the loop to end at a point ! but at what point ?
wont it just get increased by 1 forever ?

1 Like

wont it just get increased by 1 forever ?

From the docs: for (initialization; condition; afterthought).

It’s that condition bit that’s the stopper.

Consider the most basic loop, the while loop: while (condition):

let count = 0; // initialization
while (count<5 /* condition */) {
    console.log(count);
    count++; // afterthought
}

The for loop is basically just a way of combing those common loop elements in a single place:

for(let count = 0; count<5; count++) {
    console.log(count);
}

As a result, a for is preferred when those are the looping elements. A while is used for loops where those elements aren’t in play.

Note, a for is really just organizational. You can turn it back into a while:

let count = 0
for(; count<5;) {
    console.log(count);
    count++;
}

Or even just use it as an infinite loop wrapper:

let count = 0
for(;;) {
    console.log(count);
    if(!(count++ < 5)) { break; }
}
1 Like

thanks, now i get it

1 Like