Data Types in Javascript
There are two types of data types available in Javascript.
PRIMITIVE DATA TYPE
This can store only a single value at a time. Primitive data types are also known as in-built data types.
Below is a list of primary data types with proper descriptions and examples:
1)Boolean
It represents the logical value of an object or statement and can be assumed to be “true” or "false.”. The boolean data type can accept only two values, i.e., true and false.
//Example - 1
let isUserEligible = true;
if(isUserEligible){
console.log('You are eligible to vote')
}
Output: You are eligible to vote
//Example - 2
let firstNumber = 5;
let secondNumber = 10;
//value will be assigned from the comparision
let isFirstNumberBigger = (firstNumber > secondNumber)
if(isFirstNumberBigger){
console.log('first number is bigger than second');
} else {
console.log('second number is bigger than first');
}
O/P: second number is bigger than first
2)Null
A null is another type of value that indicates that the variable is empty or has no value. We should manually assign null to make a variable empty. It indicates that the variable points to an object.
let something = null;
console.log(something); //null
3)undefined
This default value will be assigned by JavaScript when we declare any variable without assigning the value. A function returns undefined if the function returns no value.
领英推荐
let something;
console.log(something); //undefined
4)Number
Number type represents both integer, float, positive, and negative values.
We can perform all arithmetic operations on numbers like (+, _, *, /, %)
let age = 25;
console.log(age); //25
let totalMarks = 78 + 65 + 54 + 98 + 87 + 77;
console.log(totalMarks); //459
let average = (totalMarks/6);
console.log(average) //76.5
5)String
A string is a collection of alphabets or characters. A string is surrounded by quotes. We have 3 types of quotes.
let firstname = 'ganesh';
let lastName = "manam";
There is no difference between single and double quotes, but we use back-ticks for string interpolation.
Interpolation-Dynamically replacing variable values in strings called interpolation.
let firstName = 'Ganesh';
let lastName="Manam"
let welcomeText = `Welcome ${firstName} ${lastName}, Have a nice day!`
console.log(welcomeText);
O/P: Welcome Ganesh Manam, Have a nice day
Strings have a lot of utility methods, we will discuss those methods in the next article.
NON-PRIMITIVE DATA TYPES
This data type can store multiple values at a time. Array and Object are examples of non-primitive data types. We will discuss about this in the next article.