JavaScript – Primitive Datatypes
Primitive Datatypes are those which are represented directly at the lowest level of language implementation. There are six primitive datatypes in JavaScript. They are :
- string
- number
- boolean
- null
- undefined
- symbol
Primitive data types does not have any methods.
JavaScript provides wrapper objects for the primitive datatypes with methods implemented.
Primitive Datatype | Wrapper Object |
string | String |
number | Number |
boolean | Boolean |
symbol | Symbol |
For the primitive datatypes : null, undefined : there are no Wrapper Objects.
Initialize variables with primitive values
JavaScript is a dynamic language.
- Variables are not declared with a datatype.
- A variable is not fixed to a single datatype.
- A variable’s datatype changes with the value assigned to a variable.
- To know a datatype of value that is stored in a variable, use typeof variableName .
// primitive datatypes
var a = 1; // number
var b = 'Hello'; // string
var c = true; // boolean
var d; // undefined
var e = Symbol(54); // symbol
Note: Null datatype has only one value and that is null. In JavaScript a null value represents a reference that points, generally intentionally, to a nonexistent or invalid object or address.
Conclusion
In this JavaScript Tutorial – Primitive Datatypes, we have learnt about the different datatypes available, how to initialize a variable with one of them, and how to know a variable’s value datatype.