- Primitive Values
- Empty Values
They are the pre-defined datatypes which are categorised into three categories.
- An integer is just a whole number.
- A floating point value is a real number which has a decimal for sure.
- Exponential values use 'e' to indicate the power.
var a = 23; var b = 2.3; var c = 34e5; //which means 34 raised to the power 5.
2. Strings: Strings contain a sequence of characters, which is always written inside a pair of Double("...") or single('...') quotes.
var place = "India"; var room = 'ABC@123';
3. Boolean: Boolean datatype includes only two values, True or False. Two keywords true and false are used to represent these boolean values. In most cases, boolean values are used as an outcome of the evaluation of an expression.
<script> var cmp = 4<6; document.write(cmp); </script>
Here, the result of (4<6) is stored in the variable named cmp. You can clearly see in the output that the result is the boolean value true.
When a variable is empty, then it holds one of the following special values:
Both of these indicate the "no value", but the difference is that null is a keyword. In case, you check the datatype of an uninitialized variable; you will get a string 'object'.
Whereas, undefined is a predefined global variable which is initialized with a 'value = undefined'.
<!DOCTYPE html> <html lang="en"> <head> </head> <body> <script> var a = 10; var b = 12.56; var c = 'February'; var d = true; var e = null; var f = undefined; document.write(' typeof a = ', typeof a, '<br>'); document.write(' typeof b = ', typeof b, '<br>'); document.write(' typeof c = ', typeof c, '<br>'); document.write(' typeof d = ', typeof d, '<br>'); document.write(' typeof e = ', typeof e, '<br>'); document.write(' typeof f = ', typeof f); </script> </body> </html>
typeof a = number typeof b = number typeof c = string typeof d = boolean typeof e = object typeof f = undefined
In this program, five variables are created which are initialized with the different type of values. With the help of the typeof operator and document.write() method, the type of values is displayed.
You can directly use the values with the typeof operator.
number number string
Let us understand this by the following examples:
Adding a number and a string.
<!DOCTYPE html> <html lang="en"> <head> </head> <body> <script> var a = 'QueHow'; var b = 3333; var c = a + b; document.write('a + b = ', c); </script> </body> </html>
a + b = QueHow3333
Adding a two numbers and a string.
<!DOCTYPE html> <html lang="en"> <head> </head> <body> <script> document.write( 10 + 20 + '30' ); </script> </body> </html>
- Now, first of all, you may drive your attention towards <script> tag in the above code. It contains the document.write() statement which takes an expression as the argument.
- This expression adds three values that are, 10(number), 20(number) and '30'(string).
And now, look at the output, it is 3030. This shows that, the expression is evaluated in 'left to right' direction, starting with,
- 10 + 20 = 30, here both the operands are numbers, thus type conversion is not needed.
- 30 + '30' = 3030, in this case, the operands are different, number and string. Therefore, both are treated as strings.