Este objeto nos permite crear un tipo booleano, es decir, verdadero o falso , tomando valores verdaderos o falsos. Podemos crear estos objetos a través de sus constructores. Veamos algunos ejemplos:
a = new Boolean (); Asigna un valor 'falso'
a = new Boolean (0); Asigna un valor 'falso'
a = new Boolean (""); Asigna un valor 'falso'
a = new Boolean (false); Asigna un valor 'falso'
a = new Boolean (0_name); Asigna un valor "verdadero"
a = new Boolean (true); Asigna un valor "verdadero"
Very often, in programming, you will need a data type that can only have one of two values, like
For this, JavaScript has a Boolean data type. It can only take the values true or false.
You can use the Boolean() function to find out if an expression (or a variable) is true:
Boolean(10 > 9) // returns true
Or even easier:
(10 > 9) // also returns true
10 > 9 // also returns true
The chapter JS comparisons gives a full overview of comparison operators.
The chapter JS conditions gives a full overview of conditional statements.
Here are some examples:
The Boolean value of an expression is the fundament for JavaScript comparisons and conditions.
100
3.14
-15
"Hello"
"false"
7 + 1 + 3.14
5 < 6
The Boolean value of 0 (zero) is false:
var x = 0;
Boolean(x); // returns false
The Boolean value of -0 (minus zero) is false:
var x = -0;
Boolean(x); // returns false
The Boolean value of "" (empty string) is false:
var x = "";
Boolean(x); // returns false
The Boolean value of undefined is false:
var x;
Boolean(x); // returns false
The Boolean value of null is false:
var x = null;
Boolean(x); // returns false
The Boolean value of false is (you guessed it) false:
var x = false;
Boolean(x); // returns false
The Boolean value of NaN is false:
var x = 10 / "H";
Boolean(x); // returns false
Primitive values, like true and false, cannot have properties or methods (because they are not objects).
But with JavaScript, methods and properties are also available to primitive values, because JavaScript treats primitive values as objects when executing methods and properties.
For a complete reference, go to our Complete JavaScript Boolean Reference.
The reference contains descriptions and examples of all Boolean properties and methods.
Comparison and Logical operators are used to test for true or false.
Comparison operators are used in logical statements to determine equality or difference between variables or values.
Given that x=5, the table below explains the comparison operators:
Comparison operators can be used in conditional statements to compare values and take action depending on the result:
if (age < 18) text = "Too young";
You will learn more about the use of conditional statements in the next chapter of this tutorial.
Logical operators are used to determine the logic between variables or values.
Given that x=6 and y=3, the table below explains the logical operators:
JavaScript also contains a conditional operator that assigns a value to a variable based on some condition.
variablename = (condition) ? value1:value2
voteable = (age < 18) ? "Too young":"Old enough";
If the variable age is a value below 18, the value of the variable voteable will be "Too young", otherwise the value of voteable will be "Old enough".
Bit operators work on 32 bits numbers.
Any numeric operand in the operation is converted into a 32 bit number.
The result is converted back to a JavaScript number.
x = 5 & 1;
The result in x:
1
The examples above uses 4 bits unsigned examples. But JavaScript uses 32-bit signed numbers.
Because of this, in JavaScript, ~ 5 will not return 10. It will return -6.
~00000000000000000000000000000101 will return 11111111111111111111111111111010