Boolean Values
Very often, in programming, you will need a data type that can only have one of two values, like
- YES / NO
- ON / OFF
- TRUE / FALSE
For this, JavaScript has a Boolean data type. It can only take the values true or false.
The Boolean() Function
You can use the Boolean() function to find out if an expression (or a variable) is true:
Boolean(10 > 9) // returns true
(10 > 9) // also returns true10 > 9 // also returns true
Everything With a "Value" is True
Example :
100
3.14
-15
"Hello"
"false"
7 + 1 + 3.14
3.14
-15
"Hello"
"false"
7 + 1 + 3.14
Everything Without a "Value" is False
var x = 0;
Boolean(x); // returns false
Boolean(x); // returns false
Booleans Can be Objects
Normally JavaScript booleans are primitive values created from literals:
var x = false;
But booleans can also be defined as objects with the keyword new:
var y = new Boolean(false);
Example :
var x = false;
var y = new Boolean(false);
// typeof x returns boolean// typeof y returns object
var y = new Boolean(false);
// typeof x returns boolean// typeof y returns object
0 comments:
Post a Comment