You are currently viewing JavaScript Data Types

JavaScript Data Types

Variables in JavaScript

Before moving to JavaScript Data types first we will read about JavaScript variables. Variables are the container for storing data values. It is a name of the storage location where the data is actually stored.

Declaration of JavaScript Variable

There are 2 ways by which we can declare the JavaScript variables

  • By using var
  • By using let

Example

var a = 2;
var b = 6;
var c= a*b;
console.log("a is: " + a);
console.log("b is: " + b);
console.log("c is: " + c);

let a = 2;
let b = 6;
let c= a*b;
console.log("a is: " + a);
console.log("b is: " + b);
console.log("c is: " + c);

Rules for the naming of JavaScript Variable

  • Variable names must begin with a letter. It can also begin with _ and $ symbol.
  • The first character of variable name must not be a digit.
  • Variable names can contain letters, digits, underscores, and dollar signs.
  • Variable names are case sensitive (A and a are different variables, not same )
  • JavaScript keyword cannot be used as variable names for example let, class, return  are reserved in JavaScript. 

Data Types in JavaScript

There are two types of data types in JavaScript

  • Primitive data type

Primitive data type includes String ,Numbers ,Boolean , null, Undefined , Symbol.

  • Non-primitive data type

Non-primitive data type includes Arrays, Object literals, Dates.

Data TypesExplanationExample
StringString is used to store text . It is a sequence of characters.
String content is wrapped inside ' ' or " ".
let a= ‘JavaScript’ ,
let b= “JavaScript”
NumbersNumber represents numeric value like Integer and Float.let a = 10,
let b = 10.5
BooleanBoolean represents two values true or false .let a =true ,
let b = false
nullnull represents empty value.
It means the variable a is empty.
let a= null;
UndefinedVariable without any value is undefined.let a;
ArraysArray store multiple value of the same data type.
Arrays are written with square brackets and array items are separated by commas.
let a = [“abc'”, “xyz”]
Object literalsObject properties are written as key : value pairs and separated by commas and end with semicolon.
It is a complex data type.
let a = {
name: “xyz”,
marks: 12
};
DateDate object represents date & time. It includes days, months, years, hours, minutes, and seconds.let date = new Date(“28 July 2022”);
JavaScript Data Types

JavaScript typeof

To find the data type of a variable we use of typeof Operato in JavaScript.

For Example

let name = 'Mohan';
typeof(name); // returns "string"

let val = true;
typeof(val); //returns "boolean"

let marks = 44;
typeof(marks); //returns "number"

const  x = null;
typeof(x); // returns "object"

Before JavaScript Data types tutorial read about JavaScript introduction . JavaScript is a dynamic type language. It automatically determines the variables data type. This is all about JavaScript data types and variables.

Written By : Ankita Kataria.
Want to learn Python concept check this Python Quick Start.

Check all tutorials here CodeShruta.

Leave a Reply