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.
For example, if var tempFahrenheit = 99, variable tempFahrenheit is of type number. But if,
var tempFahrenheit = “Ninety Nine”, variable tempFahreheit is of type string.
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.
To store a variable that holds a numeric value, the primitive data type number is used.
Constant of type number can be declared like this:
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.
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.