Why this works only inside a function?


The below code works:

var myName = "Mihai"
var name = function()
while (myName.length = 5) {
console.log("hello " + myName)
myName.length > 5

but if I use the while statement outside the function if will be an infinite loop

var myName = "Mihai"
while (myName.length = 5) {
console.log("hello " + myName)
myName.length > 5



Both are an infinity loop, but function don't get executed unless you call them, which i guess you didn't do. So, the function doesn't get executed, so i am going to state the obvious here, neither is the loop, which might make it look like you don't have an infinity loop.

I am not sure what you are doing on this line:

myName.length > 5

could you explain? So far i can see, it doesn't do anything. Are you trying to update the loop condition?

Then the big issue, this line:

while (myName.length = 5)

you assign a value of 5 here, rather then comparing with 5 (two equal signs)


If you define a =function= nothing happens....
You will have to call the =function=
to execute the pre-defined =function-code=
it would be then that you would run into the infinite loop

the while-loop:

The WHILE-loop will continue it's loop
as long it's condition evaluates to a Boolean true

An what do you do......
you try to assign a number Value to
the read-only length property of the string Value which you hide behind the variable myName

The only way you would get a different length property-Value
would be to change the string Value

var myName = "Mihai"
while (myName.length === 5) {
   console.log("hello " + myName);
   myName = "codesurfer51309";


+++ while loop explained +++++++++++

What they are trying to achieve,
that you are able to use a while-loop.

             point of entry
             in while-loop
 ->  while ( condition===true )   -->EXIT--->if condition===false
|       {  // begin of while-code-block  
|          // your code that will be executed 
|          //       as long as while-condition === true
^<- - - } //end of while-code-block, 
                     while-condition is checked again !!

They give you the code:

var understand = true;

	console.log("I'm learning while loops!");
	//Change the value of 'understand' here!
	understand=false;   //<<== you make sure

1 You assign true to the variable understand

2 The while-statement -tests the condition-,
--which is the variable understand with its value being true
3 Access is granted to the while-code-block
4 console.log("I'm learning while loops!"); is executed.
5 assignment of the false value to the variable understand
6 END of while-code-block is reached,
goto 2 //The while-statement -tests the condition-, understand

As understand renders to false,
the while will NOT execute its body,
and will EXIT.


Do you want to add a counter? Execute loop while i=0; i<5; ++ ?
not sure myself.


That looks like for loop syntax, a while loop you general change the condition of a while loop inside the loop


I was just trying to create a "while" loop that has a variable not a Boolean, but a string or a number.

Understood my mistake here:
(myName.length = 5) , it should have been ===

But I didn't knew how to stop the loop. more exactly how to set the condition to false.
My thinking was that the false condition (myName.length > 5) will stop the loop, as myName.lenght ===5.

Anyways. Thanks for all the good advice!


The while loop will run as long as the condition is False. So, you want a condition, and then change it inside the loop.


myName.length > 5

won't do much, it won't stop the loop. It is just a check, without doing anyting