Javascript Data Types Explained
Introduction
When you are ordering food online, you select food by their “name” from online menu, lets say you selected ‘Cheese Burger’ which is of type string used to represent text. ‘Cheese Burger’ will have an associated cost say ‘$5’ which is a numeric value. As you proceed to checkout, option for ‘extra cheese’ pops in, which you can answer in either yes or no, or in technical term true or false.
In similar way, values in a program are expressed in different representations based on what you are using it for. These different representations for values are called data types in programming terminology.
- ‘Cheese Burger’: Value can be displayed on screen using a string.
- ‘$5’: can be represented as a number, so when you add more items, math can be done on them.
- ‘yes’ or ‘no’: A decision can be represented using a boolean (true or false).
Data Types in Javascript
The data types in javascript can be further divided as Primitive and Object. There are total of 6 primitive types defined by latest ECMAScript standard:
- boolean
- null
- undefined
- number
- string
- symbol(new in ECMAScript 2015)
and Object Type.
Primitive Types
In Javascript, all values except Object are defined as immutable values (values that cannot be changed or modified).
Boolean
Boolean represents logical entity that can only have two values, true or false. It is useful in controlling program flow and decision making using conditional statements.
var bool = true;
if ( bool ) {
//then some action;
}
Null
null represents absense of any value ie empty or non-existent value. null explicitly means nothing. Content of variable can be erased without deleting it, by assigning it to null.
var num = 45;
num = null; //num contains no value now
Undefined
Undefined means variable has been declared but is not yet assigned to any value.
var exp;
alert(exp); //undefined
Number
According to ECMAScript standard there is only one Number Type in Javascript. It serves for both integer and floating point. The number type have three special numeric values: +Infinity, -Infinity, and NaN(not-a-number).
- Infinity and -Infinity are special values that represent mathematical infinity and are greater than or smaller to any number respectively.
var exp = 1 / 0;
alert(exp); // Infinity
2. NaN represents a computational error. It is result of failed or incorrect mathematical operations.
var exp = "hackinbits" / 45;
alert(exp); // NaN
Any further operations on NaN will give NaN
var exp1 = "hackinbits" / 2;
var exp2 = exp1 + 3;
alert(exp2); // NaN
String
Strings are used to represent text. Javascript strings are immutable, that means once a string is created, it is not possible to modify it.
var exp = "Welcome to hackinbits.com";
Symbol
Symbols are new to Javascript. Symbols are unique and immutable primitive value. They are used to create unique identifiers for objects (more on it in another article, as it can be explained better after discussing few topics first).
Object Type
Object type refers to a compound value that can be seen as collection of properties. Properties are key/value pairs. Keys are strings (or Symbols) and values can be of any type, including other objects.
var obj = {
name: 'Cheese Burger',
cost: 5,
extra_cheese: true
};
You would have noticed that the key extra_cheese is written with ‘_’ rather than using a space. Space can be used as well but then key should be quoted in “”(double quotes like “key”), example :
var obj = {
name: 'Cheese Burger',
cost: 5,
"extra cheese": true
};
Using typeof operator for determining types
The typeof operator helps in finding the type of value that your variable is storing. It returns a string indicating the type of argument. It supports two syntax:
- As an operator: typeof v.
- As a function: typeof (v).
var exp = typeof "Welcome to hackinbits.com";
alert(exp); // string
Dynamic Typing
Variables in javascript are not directly associated with any particular data type. Only values have types in javascript. Variables are simple container for values. You can say, javascript have typed values and not typed variables.
var exp = 45; // exp is a Number
var exp = 'hackinbits.com'; // exp is now a String
var exp = true; // exp is now a Boolean
Summary
- There are seven basic data types in Javascript :
- boolean
- null
- undefined
- number
- string
- symbol(new in ECMAScript 2015)
- and Object Type
- Javascript has typed values instead of typed variables. Variables are simple container for values.
- We can use typeof operator for knowing the type of value a variable is storing.
If you like this article, clap a few times. More interesting articles on its way, follow Hackinbits and we will inform you when new articles will arrive.