y = 10; // Now y is a Number
y = “10”; // Now y is a String
- Primitive Type (String, Number, and Boolean)
- Composite or Reference Type (Object, Array, and Functions)
- Special Data Type (Null or Undefined)
Let’s discuss these types more thoroughly with examples.
Primitive data types are simple basic blocks of any language, having only one value assigned to them at a time. They consist of:
String Data Type
A sequence of characters enclosed by single or double quotation are represented through string data type. The string consists of numbers, names, and quotes, as long as they are within the quotation mark.
var a = ‘Hello World’; // using single quotation
var b = “My Name is..”; // using double quotation
var c = ‘We‘re be very pleased.’;// escaping single quote with backslash
Number Data Type
Various types of numbers, such as negative or positive numbers with decimal places, and numbers with exponential notations are represented through this data type:
var b = 25.5; // number with floating points
var c = 1.25e+6; // exponential form, similar to 1.25e6 or 1250000
var d = 1.25e–6; // exponential form, similar to 0.00000125
Furthermore, number data types also represent special values like Infinity, -Infinity, and NaN. Infinity is produced by dividing a non-zero with 0, producing a number greater than all. Whereas, NaN shows “not a number”. This occurs through invalid or undefined mathematical operations.
console.log(–10 / 0); // Output: -Infinity
console.log(10 / –0); // Output: -Infinity
console.log(“String” / 4); // Output: NaN
console.log(Math.sqrt(–1)); // Output: NaN
Boolean Data Type
Boolean data types represent the values of yes or no(on or off) through two specific values i.e true or false. Boolean values are used for various comparison purposes.
var isHungry = false; // no, I’m not hungry.
var a = 1, b = 3, c = 5;
console.log(b > a) // Output: true
console.log(b > c) // Output: false
Composite or Reference
Composite Data type helps us to store various data types and complex entities as a collection. They consist of:
Objects Data Type
Objects help in storing various types of data as a collection. A particular key name is given to the object, which is a string. This can store various data types like numbers, boolean values and strings as a collection.
var user = “name”: “Sam”, “secondName”: “Mathew”, “age”: “25”;
// For better reading
var book =
“bookName”: “Harry Potter and the deathly hallows”,
“author”: “J.K Rowling”,
“total copies”: 10
Arrays Data Type
var languages = [“English”, “French”, “Persian”];
console.log(fruits); // Output: Orange
console.log(languages); // Output: Persian
Function Data Type
return “Hello To the Universe!”;
// type of the variable greet
alert(typeof greet) // Output is a function
alert(greet()); // Output is Hello To the Universe!
Special Data Type
Special data type is:
Undefined Data type
It contains only a single value. This happens when a variable is not assigned with any value, so at the time of interpretation, the output is displayed as ‘undefined’.
var y = “My Name is John”
alert(x) // Output: undefined
alert(y) // Output: My Name is John
Here, the variable x was not assigned with any value. That’s why the interpreter showed the result as ‘undefined’.
Null Data type
Similarly, the null data type also contains a single value. But, here in the case provided below, the value is assigned as “null”. So that it’s easily understood by the user that no value is assigned.
var a = null;=
alert(a); // Output will be null
Each type is explained with examples for better understanding and implementation. This makes storing and manipulation of data easier for the user.