There is a syntax error saying "expected ' ; '" but i cant figure out where and what is wrong with these lines of code! Please answer ASAP!


#1

var i = 0;
for (i < 2; i++) {
console.log(i);
};
while (i = 0) {
console.log("I don't know a thing about coding!");
i = 2;
};
do {
confirm ("Do you take back the statement?");
}while (i = 0);


#2

3 things,

First

:
this is not how you do a for loop:

this is how:
for(i=0;i<2;i++)

Second

you do the comparator opertar so not one = but two ==:

to:
while(i==0)

Third

same thing as second two ==:

}while(i==0)


#3

can u explain to me all three?
1. ive already stated the variable "I" so its a global variable and can be stated anywhere.
2. what is the difference between i=0 and i==0???


#4

Difference between "=" and "==":
"=" is a reference, actually "=" invoke builtin function id() which return internally memory address location for an object.
"==" is just arithmetic equal.


#5

I thought === was equal


#6

for (i < 2; i++) { do something}; --> you missed ;
for (; i < 2; i++) { do something}; --> correct


#7

Actually is an comparison operators, sorry


#8

k I understand now thx a lot man u rock!


#9

== and === are both operators that check for the equality of 2 values the key difference is that === is more strict and not only checks for the values but also for the types e.g.

42 == "42" --> true, same value 42 
42 === "42" --> false, same value 42 different types number vs String

As @echosomething already mentioned you can leave out up to all 3 parts of the for loop (infinite loop) but you always have to have 2 semicolons in the () of the for loop to separate the 3 parts of it even if you don't directly state them. Last but not least loops do not require a semicolon at the end of their }.