Data Types in Javascript

Data Types in Javascript

There are two types of data types available in Javascript.

  • Primitive Data type (boolean, null, number, undefined, string)
  • Non-Primitive Data type (array, object)


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.

  1. ' ' - Single quotes.
  2. " " - Double quotes
  3. - Backticks

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.

要查看或添加评论,请登录

Ganesh Babu Manam的更多文章

  • How to declare variables in Javascript

    How to declare variables in Javascript

    Variables are containers for storing data Variable is a place where we can store values in our program for later use…

  • Javascript- The heart and soul of Web Development

    Javascript- The heart and soul of Web Development

    What is JavaScript? JavaScript is a scripting language used to create interactive and dynamic websites. Scripting…

  • How to push code to GITHUB

    How to push code to GITHUB

    Signup to Github Sign in to Github Once in a computer Download git software Open terminal and verify git installation…

社区洞察

其他会员也浏览了