Different types of data can be stored in a variable in JavaScript. We can store numeric,boolean, decimal and other kinds of data in a variable. The kind of value a variable can store is defined in different categories depending upon certain behaviour known as Data Types.
The Data Types are further classified in 2 parts:
a. Primitive Data Types
Primitive data types are a basic data types that represent single and immutable values. JavaScript has 7 primitive data types:
1 Number: Represent both integer and floating-point numbers like
2 String : It represents sequence of characters. It represents textual data like words and sentences. String must be enclosed in single('') or double("") qoutes.
3 Boolean: represents true or false
4 undefined: If we declare a variable but do not assign any value its value is 'undefined'
5 null: null represents an intentional absence of valueIt is explicitly assigned by the programmer to indicate that a variable currently has no valid object or data.
6 BigInt: Used to represent very large integers beyond the safe limit of Number.
7 Symbol : Symbols are a primitive data type used to create unique and immutable values.
b. Non-Primitive(Reference) Data Types
These data types can store mutiple values and are mutable at the same time.
1 Objects: Objects are used to store values in key-value pairs. They are defined as:
2 Array: Arrays are used to store ordered collections of values.
Arrays have indexing . Indexing is the way JavaScript comes to know that which value you want to access from the array.
Indexing in JavaScript starts from 0(not from 1)
you can see the the indexes and the values corresponding to that particular index in the table given below.
When we tried to print the value arr[1] the output comes as 'Red' which is expected since the value at index 1 in the given array is 'Red'
| Index | Value |
|---|---|
| 0 | Orange |
| 1 | Red |
| 2 | Blue |
The values within an array can be of different data types including both primitive and on-primitive data types.
You can have objects and arrays within arrays.
3 Functions: It are used to write a piece of code which gets executed when we all them.
Functions provide reusability. You can use them wherever and whenever you want, you just need to call it like function_name()