JavaScript Primitive Datatypes
Primitive Datatypes
Datatype :
To be able to proceed with the manipulation of the data assigned to the variables, it is mandatory for a programming language to know the type of value or the type of data the variable is holding. Because the operations or manipulations that have to applied on a variable will be specific to the type of data a variable holds.
For example, if we need to apply add operation on two variables, the result will vary based on the fact whether the value of both the variables is numeric or textual.
To handle this situation data types are used.
Let us see what exactly we mean by data types
Datatype mentions the type of value assigned to a variable.
In JavaScript, the type is not defined during variable declaration. Instead, it is determined at run-time based on the value it is initialized with.
Hence JavaScript language is a loosely-typed or dynamically-typed language.
We can have the same variable of different types in JavaScript code based on the value that is assigned to it.
For example, if var tempFahrenheit = 99, variable tempFahrenheit is of type number. But if,
var tempFahrenheit = “Ninety Nine”, variable tempFahreheit is of type string.
JavaScript defines the following data types:
Primitive type: The data is said to be primitive if it contains an individual value.
Non-primitive type: The data is said to be non-primitive if it is a collection of multiple values.
Number :
To store a variable that holds a numeric value, the primitive data type number is used.
In JavaScript, the data type number is assigned to the values of type integer, long, float and double. For example, the data type for values 300, 20.50, 10001 and 13456.89 is number.
Constant of type number can be declared like this:
In Javascript, any other value except for the type of values mentioned above is not a legal number. Such values are represented as NaN (not-a-number).
String :
When a variable is used to store textual value, primitive data type string is used.
The string represents textual values.
string values are written in quotes, single or double.
Single quotes or double quotes have no special semantics over the other. Though you have to be strategic in the selection between the two when you have to use one string within the other.
Example: var companyName = “bookofnetwork‘s”; OR var companyName = ‘bookofnetwork's’;
This will be interpreted as bookofnetwork's and bookofnetwork‘s respectively. But, if you use same quotes for both the strings:
Example : var companyName = “bookofnetwork's”; OR var companyName = ‘bookofnetwork‘s’;
It is a syntax error.
Thus, remember, when you have to write string within a string, use different quotes for both.
To access any character from within the string we need to be aware of its position in the string. Each character in the string occupies a position.
The first character exists at index 0, next at index 1, and so on.
boolean :
When a variable is used to store a logical value that can be only true or false at all the times, primitive data type boolean is used.
boolean is a data type used to represent only 2 values.