Why the while loop works with < but not with ===?


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-ASGIv/1/1?curriculum_id=506324b3a7dffd00020bf661


Why do this code below works with counter < 3, but it doesn't work with counter === 3 ?


var counter = 0

var loop = function(){
	while(counter === 3){
		//Your code goes here!
		console.log("I'm looping!")
		counter++		
	}
};

loop();


#2

Your code should not work for syntax errors.
If I understand it correctly, it works for <3 as the logic inside while loop is valid for < 3. Remember you defined:
var counter =0.
So, it can go up with the counter++ while the logic is valid.

But for counter ===3 and var counter =0 together do not make sense at all or while loop does not get any logic to execute.


#3

Oh, let me see if I get it. Is it because when I declare counter === 3 in the condition, the counting with the ++ starts at 3? Or is it because I can't declare an equality in the variable and at the same time declare equality in the condition for the same variable? Or putting it differently, is it because < and > admit ++, but === doesn't admit ++?


#4

When you are saying var counter =0; counter holding a value which is 0.
In the logic, while (counter ===3)
Counter (whose initial value is 0) is not holding a value 3. Does not satisfy the logic so should not execute while loop.
But if (counter < 3) then when you are passing counter(whose initial value is 0) to the function satisfy the condition and that's why it works.

counter = 0;

var loop = function()
{
	while(counter < 3)
               {
	  console.log("I'm looping!");
	  counter ++;
	  	}
	 
};
loop(counter);

The instruction says you can do this however way you want but the hint wants us to do using only while. I also used a for loop inside a while loop but that's not very appropriate for this exercise. The main thing if you can distinguish each loop and (for, while, do while etc) their purposes.


#5

You see, it won't work because in the lessons, you have to follow the instructions**exactly**, or you will get the lesson wrong. That is why you could do

, but not