Data Types
- JavaScript variables can hold different data types
- In JavaScript you have two categories of data types: primitives or value types and reference types
Primitive Data Types/Value Data Types
- String
- Number
- Boolean
- undefined
- null
- Symbol (From ES6)
Literal
- Literals represent values in JavaScript. These are fixed values that you literally provide in your script.
String
- String is a sequence of character
- You can single or double quotes
let name = "kumar"; //String Literal
Number
- Can be with or without decimal
- Represent both integers and floating-point numbers
let height = 6; //Number Literal
Boolean
- Possible values are true and false
let isGraduated = true; //Boolean Literal
Undefined
- if you don't initialize a variable, the default value is undefined.
- Undefined is a type and you can also assign it a value
undefined
. - The meaning of
undefined
isvalue is not assigned
.
let bikeName;
let bikeName = undefined;
Null
- Use null when the value is not known
- Use null in situations when you want to explicitly clear the value of a variable.
- It’s just a special value which represents
nothing
,empty
orvalue unknown
. - The result of
typeof null
isobject
. That’s an officially recognized error intypeof
, coming from very early days of JavaScript and kept for compatibility.
let selectedCar = null;
selectedCar = "maruti"
selectedCar = null;