JavaScript Data Types
Data types define what types of data can be stored and manipulated in a program.JavaScript provides different data types to hold different types of values.
There are two types of data in JavaScript. Which are as follows
- Primitive data types
- Non-Primitive data types
We will now discuss in detail the data types of JavaScript.
Primitive data type
Primitive data types include the following types.
- String
- Number
- Boolean
- Symbol
- BigInt
- Null
- Undefined
Non-Primitive data type
There is only two type of non-primitive data type which is as follows
- Object
- Array
We will now discuss the types of primitive data types in detail
JavaScript Strings data type
strings are written with quotes. You can use single or double quotes
You can learn about strings in the following example.
Example
Edit it yourselfvar india = "Dilhi";
var china = 'Beijing';
document.write( india +" "+ china )
JavaScript Number data type
The type of number represents both an integer and a floating point number.
There are many actions for numbers, e.g. Multiply *, Divide /, Add +, Subtract -, and so on.
Example
Edit it yourselfvar x1 = 10; // integer
var x2 = 10.5; // floating-point number
var x3 = 20.1e+5; // exponential notation
var x4 = 1.10e-5; // exponential notation
JavaScript Boolean data type
This data type represents logical units. Boolean represents one of two values: true or false.
The following example illustrates how a boolean works
Example
Edit it yourselfvar x = 5;
var y = 5;
var z = 6; //output- x == y-true, y == z-false
JavaScript Symbol data type
Symbol type is used to create unique identifiers for objects. For the sake of completeness we have to mention it here, but we postpone the details till we know the things.
Example
Edit it yourselfconst company = {
car1: "maruti",
car2: "audi",
car3: "renjo",
car4: "suzuki"
};
JavaScript BigInt data type
Bigint is used for integers of arbitrary length
Bigint type 253-1
represents numbers greater than.The letter n
is added to the end of a large literal number:
Example
Edit it yourselfvar bigint = 12345678901890n;
document.write(bigint); //return 12345678901890
JavaScript Null data type
Null in JavaScript means "nothing". It is believed that which does not exist. You might consider it a bug in JavaScript that typeofnull
is an object.
You can make the value of the object zero or null by turning the object into null.
Understand the null datatype in the following example
Example
Edit it yourselfvar a = "javascript tutorial";
a = null;
document.write(a); // output: null
JavaScript Undefined data type
Null in JavaScript means "nothing". It is believed that which does not exist. You might consider it a bug in JavaScript that typeofnull
is an object.
You can make the value of the object zero or null by turning the object into null.
Understand the null datatype in the following example
Example
Edit it yourselflet car1= "audi";
let car2;
document.write(car1 + "<br>" + car2); //car2 value is undefined
We will now discuss the types of Non-Primitive data types in detail
JavaScript Object data type
In JavaScript, an object is an unaltered collection of key-value pairs. Each key-value pair is called a property.
The key to the property could be a string. And the value of the property can be any value, e.g., string, number, array and even function.
JavaScript provides you with many ways to create an object. The object is most commonly used to use a literal sign.
An object can be created with figure brackets {...} with an optional list of properties. Object Properties Name: Value pairs, separated by commas.
Example
Edit it yourselfvar country = {
cityName: "India", capital: "Dilhi", pm: "narendra modi", states: 28,
};
document.write(country.cityName +" "+ country.pm )
How to creat object is stated in the example above.
In the example above the object (country) has four properties: cityName, capital, pm and states.
The "country" is considered as an object as stated in the example
JavaScript Arrays data type
An array is a type of object that is used to store multiple values in a single variable.
JavaScript arrays are written with square brackets.
JavaScript provides you with many ways to create an object. The object is most commonly used to use a literal sign.
The array index starts at 0, so the first array component is arr [ 0 ] not arr [ 1 ].
Example
Edit it yourselflet name = ["vikram", "hardik", "hitesh"];
document.write(name[0]); //output- vikram
- So get a brief overview of what JavaScript data tapes are and what its types are as mentioned above. We will now discuss all these types in detail in the next lecture.