-
Notifications
You must be signed in to change notification settings - Fork 23
/
booleans.js
41 lines (32 loc) · 1.72 KB
/
booleans.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
------------------------------------------------------------------------------------
Tutorial: Booleans in JavaScript
------------------------------------------------------------------------------------
*/
// Definition:-
// The boolean (not Boolean) is a primitive data type in JavaScript. It can have only two values: "true" or "false".
// It is useful in controlling program flow using conditional statements.
// DECLARATION:-
var YES = true;
var NO = false;
// CHECK TYPE OF VARIABLE WITH "typeof" OPERATOR:-
console.log(typeof(YES)); //will print "boolean" in the console
console.log(typeof(NO)); //will print "boolean" in the console
// COMPARISION OPERATORS AND BOOLEANS:-
//The comparison expressions return boolean values to indicate whether the comparison is "true" or "false".
//For example, the following expressions return boolean values.
var result = 1 > 2; //result stores "false"
// BOOLEAN FUNCTION:-
//JavaScript provides the Boolean() function that converts other types to a boolean type.
//The value specified as parameter to the Boolean() function will be converted to a boolean value.
//The Boolean() will return "true" for any non-empty, non-zero, object, or array and will return "false" otherwise.
var x = Boolean("Hello"); // x stores "true"
var y = Boolean(10); // y stores "true"
/*
------------------------------------------------------------------------------------
Challenge:-
// 1. Check the boolean value of "0" and "-0" using the Boolean() function.
// 2. Check the boolean value of "" (empty string) using the Boolean() function.
// 3. Check the boolean value of "[]" (empty array) using the Boolean() function.
------------------------------------------------------------------------------------
*/