Datatypes in JavaScript

Every programming language has a set of datatypes which can be operated upon by the programs. So has JavaScript. However, JavaScript does not restrict its variables to store a particular type of value only. A variable, previously holding an integer, may store a text string later. Thus, we can say that variables in JavaScript are untyped.

Well, JavaScript deals with three types of values:

  1. Primitive Values
  2. Empty Values
  3. Objects

Primitive Datatypes in JavaScript

They are the pre-defined datatypes which are categorised into three categories.

  1. Numbers
  2. Strings
  3. Boolean

1. Numbers: In JavaScript, a number may be an integer or a floating point value or an exponential value.

  • 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.

Example:

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. 

Javascript detects the beginning and end of the string with the help of the quotation marks.

Example:

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.

Example:

<script>
var cmp = 4<6;
document.write(cmp);
</script>

Output:

true

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.

Empty Values in JavaScript

When a variable is empty, then it holds one of the following special values:

  • null
  • undefined

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'.

Objects in JavaScript

An object type value is a collection of primitive values together with some properties. A value which is not a number, string, boolean, null or undefined, may be considered as an object, for example, an Array. Object itself is a fundamental and vast concept in JavaScript which you can find here: JavaScript Objects.

Detecting the datatype of value in JavaScript

Similar to other programming languages, JavaScript can also identify the datatype using the typeof operator. This operator can work with both, the value itself or the variable holding that value.

Syntax:
typeof value/variable

Example:

<!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>

Output:

typeof a = number
typeof b = number
typeof c = string
typeof d = boolean
typeof e = object
typeof f = undefined

Explanation:

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.

Example:

<!DOCTYPE html>
<html lang="en">
 <head> </head>
 <body>
  <script>
   document.write(typeof 100, '<br>');
   document.write(typeof 1.5, '<br>'); 
   document.write(typeof 'Learn JavaScript');
  </script>
 </body>
</html>

Output:

number
number
string

Type Conversion in JavaScript

JavaScript is capable of dealing with the unusual combinations of datatypes. For example, the addition of a number and a string. In such cases, JavaScript converts the inappropriate(in that situation) datatype into the suitable one. Although, it may give you the unexpected outputs. This phenomenon is knowns as Coercion or Type Conversion.

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>

Output:

a + b = QueHow3333

Here, on adding a string with a number, JavaScript automatically changes the number into the string.

Adding a two numbers and a string.

<!DOCTYPE html>
<html lang="en">
 <head> </head>
 <body>
  <script>
   document.write( 10 + 20 + '30' ); 
  </script>
 </body>
</html>

Output:

3030

Explanation:

  • 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,

  1. 10 + 20 = 30, here both the operands are numbers, thus type conversion is not needed.
  2. 30 + '30' = 3030, in this case, the operands are different, number and string. Therefore, both are treated as strings.