While syntax: why my code crashes the browser


#1

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

it is in the while syntax course. and i am thinking about the condition of the while loop. i have no better idea.
however, this condition makes my browser crashed down.
would you like to give me some guidance?

var understand = true;

while(understand = true){
	console.log("I'm learning while loops!");
	understand = false;
}


#2

It's possible that you're entering an infinite loop when you run your code.

Just remove the = true bit of your condition, so your while loop condition is simply while(understand).

If this solves your question, please be so kind as to mark this answer as the solution to your question. Thanks :slight_smile:


#3

There's a difference between understand = true and understand == true. The former assigns the true value to understand, the latter returns a Boolean value, i.e true or false.


#4

this solution works. thanks a lot. however i don't understand why the condition can just be simple" understand". would you please to explain?

i have another question that once i declared a variable like "var understand = true", if i want to change this variable's value, i just need to type like below:
"var understand = true;
understand = false;" is it right?


#5

you said the former assigned the true value to understand. but it doesn't has the quote, like "true". and the later doesn't use "===" which is used for boolean value in my mind. sry, i didn't get you. would you like to explain more?


#6

i have another question that once i declared a variable like "var understand = true", if i want to change this variable's value, i just need to type like below:
"var understand = true;
understand = false;"
then the value of understand becomes false. is it right?


#7

Yep, you're exactly right! The syntax to change a variable's value is just variable_name = [value], so in this case it would be understand = false.

Now to explain why it can simply be while(understand). It programming languages, when you want to check if a condition is true you can just do if(variable) or while(variable), etc...
This is because it's the same as writing if(variable == true) or the while loop equivalent. It's just a shorter version.

If this solves your question, please be so kind as to mark this answer as the solution to your question. Thanks :slight_smile:


#8

you explain so clearly! thanks a lot.

one more question, i notice that you use "==". till now, i only have used = and ===, the former is to assign value, the later is to compare. what does == mean?


#9

Using == basically means the same thing as === when you're beginning, I guess it's just the Python side of me that prefers to use == haha :slight_smile:

JavaScript has strict and type-converting comparisons. I'm not great at explaining this subject, so I'll quote developer.mozilla.org.

  • Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
  • Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another.
  • Two Boolean operands are strictly equal if both are true or both are false.
  • Two distinct objects are never equal for either strict or abstract comparisons.
  • An expression comparing Objects is only true if the operands reference the same Object.
  • Null and Undefined Types are strictly equal to themselves and abstractly equal to each other.

The equality operator (==) converts the operands if they are not of the same type, then applies strict comparison (===).

Read more about it at the Mozilla Developer website: Comparison Operators

Hope this helped!


#10

(When you use a number in a condition, as we did earlier, JavaScript understands 1 to mean true and 0 to mean false.)
the original code " var understand = value" and js knows it means true. but it doesn't use ==. why?


#11

This is because a single equal sign (=) is reserved for assigning values to variables, while the double-equals sign (==) is reserved for comparisons.


#12

then ===, three equal sign also mean comparisons, right ?


#13

Yes, but read the webpage I referenced for more information.


#14

"true" and true are different types of datatypes. The first is a string, the second is a Boolean.

Assigning values using = would work like this:

x = "true"
console.log(x) // logs true
y = true
console.log(y) // logs true

Comparing with === would work like this:

console.log(1 === 2) // logs false
console.log(2 === "2") // logs false, because they are 
// different datatypes, one is integer, the other is a string

== and === differ in that the latter involves a stricter testing, see below:

console.log(2 === "2") // logs false, because they are different datatypes
// as explained above
console.log(2 == "2") // logs true

#15

Very well explained.


#16

oh! i get it now!
thank you very much, help me a lot. have a nice day !


#17

i am sorry that i have another problem now. why the code blow is also wrong?
"
var understand = true;

while(understand){
console.log("I'm learning while loops!");

understand = "a";

}
"


#18

You're setting it to a string, which it doesn't like when the while loop checks if it's a boolean value of true.


#19

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.