Is the default JS boolean value true or false?


#1



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


No errors, just curious. :slight_smile:


This code works as expected. It runs once then stops because the while condition is not satisfied.

var myCondition = false;

var getToDaChoppa = function(){
  // Write your do/while loop here!
  do {
      console.log("You only live once!")
  } while (myCondition === true);
};

getToDaChoppa();

But this code also works, runs once and then stops, and that's unexpected (to me).

var myCondition = false;

var getToDaChoppa = function(){
  // Write your do/while loop here!
  do {
      console.log("You only live once!")
  } while (myCondition);
};

getToDaChoppa();

I've used Google and it appears the default Boolean in JS is false, which is confusing if that's correct.

In the second code block, if while (myCondition) is false by default then that loop should run infinitely? Because the variable value is set to false and the while condition is false; however it doesn't.

If it is 'true' by default then does that mean I I can always omit the === true comparison when evaluating true conditions?

If the default doesn't matter and omitting the === check simply leaves the global value unchanged then I would also expect the code in block 2 to run forever. The variable is set to false and the condition is false too.


#2

The truth is that you don't really have these "types" in javaScript that you might know from other languages. Meaning when you'd use:

bool isSomething;

you made sure that the series of bits stored there is a boolean and could get a default value for it or the value is just determined by entropic noise.
But in javaScript the type of a variable is not defined beforehand (you always use var) but instead is determined by the value you save in this variable. If you assign a string the type is string, if you assign a number the type is number and if you assign a boolean the type is boolean. Therefore the general concept of a default value doesn't apply in this context.

But their is a different case where you actually have an expected type and that is when we're using conditions. Conditions always expect a boolean, whether it is a simple boolean like true or false, or a complex expression like:

2<x -y+4 && y < 5 || x != 12

in the end it always boils down to being true or false. So in you're case it is pretty simple you set myCondition to false therefore the truth-values of myCondition when evaluated is false so you stop the loop after the first run.
In case you're condition is a boolean you can easily prove that you can abbreviate variable === true with just variable as:

variable === true is true when variable is true and
variable === true is false when variable is false

So I hope this answered you question. But as you're asking about default values maybe let's also have a look at what happens when the input is not a boolean e.g. while("true"), while(""), while(42) well in these cases you need to look up how the behave:
http://www.sitepoint.com/javascript-truthy-falsy/
Did you meant this with default value or the stuff above?


#3

you say this:

So, this means: if the condition evaluates to false, the loop ends. (loops run as long as the condition evaluates to true)

Uhm... you give your myCondition a Boolean of false:

var myCondition = false;

this has little to do with the default Boolean.

Both these conditions are false:

myCondition
myCondition === true

myCondition holds a Boolean value (false), so lets do this:

false
false === true

i replaced the variable with the boolean value. well, false is obviously false. And false doesn't equal true, so that is also false.

A loop runs, as long the condition is true. Both your cases are false, so the loops runs once


#4

Thanks for your explanations moderators - much appreciated! BUT (see below) :slight_smile:

@haxor789 - I meant the default values; I wasn't even thinking about other data types, but you make a good point. That sitepoint article was interesting if a bit over my head. The comments were most interesting as it seems even experienced folks get confused by this true/false quandary. When will things run; it seems like you have to just test it to be sure.

@stetim94 - I also appreciated your comments but there are still some things that just don't make sense to me, as a questioning newb.

For example. If the default value without a comparison operator is always false, then why does this code not generate an infinite loop?

var myCondition = false;

var getToDaChoppa = function(){
  // Write your do/while loop here!
  do {
      console.log("You only live once!")
  } while (myCondition);
};

getToDaChoppa();

myCondition = false
and
(myCondition) is also false

false === false is true - so this loop should run forever, no?

It looks like when the comparison operator is left off it just defaults to whatever the global value was. Or so I thought at first until I performed some experiments.

Here's what I've observed empirically.

var myCondition = false;

var getToDaChoppa = function(){
  // Write your do/while loop here!
  do {
      console.log("You only live once!")
  } while (myCondition === false);
};

getToDaChoppa();

This code generates an infinite loop. Expected because false === false is true

var myCondition = false;

var getToDaChoppa = function(){
  // Write your do/while loop here!
  do {
      console.log("You only live once!")
  } while (myCondition === true);
};

getToDaChoppa();

This code runs once. Expected because false === true is false.

var myCondition = true;

var getToDaChoppa = function(){
  // Write your do/while loop here!
  do {
      console.log("You only live once!")
  } while (myCondition === false);
};

getToDaChoppa();

This code runs once only. Expected because true === false is false

var myCondition = true;

var getToDaChoppa = function(){
  // Write your do/while loop here!
  do {
      console.log("You only live once!")
  } while (myCondition === true);
};

getToDaChoppa();

This code generates an infinite loop. Expected because true === true is true.

var myCondition = true;

var getToDaChoppa = function(){
  // Write your do/while loop here!
  do {
      console.log("You only live once!")
  } while (myCondition);
};

getToDaChoppa();

This code generates an infinite loop. This can only happen if 'while (myCondition);' is true. So it doesn't default to false just because it's missing a comparison operator. So I'm going to say this is expected because true === true is true.

That brings me full circle to the first code block I pasted above. The comparison operator is missing in this one. The variable starts out false.

var myCondition = false;

var getToDaChoppa = function(){
  // Write your do/while loop here!
  do {
      console.log("You only live once!")
  } while (myCondition);
};

getToDaChoppa();

It should be false === false is true and this code should run forever but it doesn't. I can only conclude then, that (myCondition) without a comparison operator is always true. So it actually is false === true is false. And it runs once. Note that in both cases the while statement with no comparison operator is true. It seems like the default variable in these cases is true, not false as I've read elsewhere. This would also support the text in chapter 4 of this lesson where only the true statement can be shortened by omitting the triple = signs.


#5

This is a infinity loop:

var myCondition = true;

var getToDaChoppa = function(){
  // Write your do/while loop here!
  do {
      console.log("You only live once!")
  } while (myCondition);
};

getToDaChoppa();

since the myCondition holds the Boolean value true. On the other hand this:

var myCondition = false;

var getToDaChoppa = function(){
  // Write your do/while loop here!
  do {
      console.log("You only live once!")
  } while (myCondition);
};

getToDaChoppa();

should not cause a infinity loop. The condition evaluates to false, the loop should end. You might want to refresh the page if just had a infinity loop.


#6

My point actually is that default values only make sense when dealing with declared (datatype name) but not defined (name = value) variables, that is at least from what I'd guess. Otherwise you already have a value and don't need a default value -> true is true and false is false, really that simple.

But in your case the:

var myCondition = X;

sets myCondition to X. So now myCondition has a value of X so there is simply no need for a default value any more (as you now have a value). So I'm a bit confused what you actually mean by this term.

And conditions aren't biased either, they just evaluate the expression in its () and if it is true they run if it is false they break. So for boolean and expressions that evaluate to boolean the game is really that simple and as explained here or at stetim94's post:

for boolean variable === true and variable behave the same. There are only 2 options for a boolean so it is rather easy to prove it :slight_smile:



Cut. Just for completion and to explain the link. As in javaScript anything is an object, they might have more properties than you see first hand and so using things apart from boolean and expressions that evaluate to boolean might as well work and maybe there is some sense to it, but it is not that easy and you should better look it up. But again that has nothing to do with a default value.



This would be a default value:

var test; 
console.log(test);

if(test){
    console.log("test");
}
if(undefined){
    console.log("test2");
}

The test variable has no value assigned to it so it is just a declared variable. Therefore it has the default "value" (more like a status or empty token but anyway) of undefined as you can see by the console.log and that behaves as false as you can see by the 2 ifs below. But again that is not what you're doing you clearly assigned true and false and true and false have values of true and false.


#7

Ah, Ok, I get it now. Thanks everyone.


#8

@calydon,
IMHO
As I read your Post

I would like to give following comment.

Javascript, if used, is an Interpreter

  • which is integrated into your Browser ( Browser-specific, Browser-version, Operating-System )
  • if the Javascript Interpreter is triggered, it will kick-in
    • into the so-called parse -phase ( given code is =parsed= doing Syntax-check, Hoisting )
    • and then comes the execution-phase

If you want to use the DO-WHILE statement
the Javascript Interpreter will expect the =syntax=

do {
    code-block =consisting of one or more statements=
   } while( condition );

From the Book ( developer.mozilla.org )
[quote]

condition

  • An expression evaluated after each pass through the loop.
    • If condition evaluates to true, the statement is re-executed.
    • When condition evaluates to false, control passes to the statement following the do...while.

[end-quote]

An expression is any valid unit of code that resolves to a VALUE.

Conceptually, there are two types of expressions:

  • 1 those that assign a value to a variable
    and
  • 2- those that simply resolve to a VALUE.

So in our case you will be using
the 2nd kind of expression
a valid unit of code that simply resolves to a VALUE
the DO-WHILE statement
evaluates the expression in a Boolean context
meaning

  • a valid unit of code that simply resolves to a =truthy= VALUE ==> true
    or
  • a valid unit of code that simply resolves to a =falsy= VALUE ==> false

==========================================================

  var myCondition = false;
  do {
      console.log("You only live once!");
  } while ();

will result in an Error given during the parse phase

SyntaxError: SyntaxisError

and the Interpreter will EXIT......

===========================================================

  var myCondition = false;
  do {
      console.log("You only live once!");
  } while (my);

will result in an Error given during the execution phase

You only live once!

ReferenceError: my is not defined

and the Interpreter will EXIT

============================================================

Reference:
google search
== the Book ==
javascript [your-question] site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/do...while
https://developer.mozilla.org/en-US/docs/Glossary/Truthy
https://developer.mozilla.org/en-US/docs/Glossary/Falsy

== discussions / opinions ==
javascript do while syntax explained site:stackoverflow.com
http://stackoverflow.com/questions/5599027/the-do-while-statement


#9

Thank you @leonhard.wettengmx.n. Some of these terms are still new concepts to me but I'm sure with time (and context) they will gain meaning.