JavaScript Data Types

Data Types is always an important concept in programming. To work on variables and using them effectively, one needs to understand what a data type is and what data types are available for JavaScript.

var length = 15;                               // Number
var lastName = "Mithilesh";                      // String
var cars = ["best", "Java", "Script"];           // Array
var x = {firstName:"Mithilesh", lastName:"Kushwaha"};    // Object

With the use of a data type, a compiler will not be able to understand what type of data is entered or used. For example consider the code- var a= 20+"Java"; now how will a compiler understand what will happen when we add value 20 to Java? Will this be resulting into error or not?

What kind of output we will get? Now if you use the above code, the output of JavaScript will result into 20Java as it will just append the value and string and takes its data type as default. There could be many uses of a data type in Javascript and its usage depends on the requirement of coder. Before we go deep into the data type, lets understand what data types are there in JavaScript.

There are two types of data types in JavaScript

  • Primitive data type
  • Non-primitive data type

JavaScript Primitive Data Type

Basically there could be four types of primitive data types namely String, Number, Boolean or Null. A string is used to represent character sequence like "New", "hello" etc. Number could be any value for example- 10,20,30 etc.

Boolean data type could be used to represent either true or false. Undefined data type can have any undefined value. Null represents no value.

Data Type Description
Number represents numeric values e.g. 100
String represents sequence of characters e.g. "hello"
Boolean represents boolean value either false or true
Null represents null i.e. no value at all
Undefined represents undefined value

JavaScript Non-Primitive Data Type

Non primitive data types could be object, Array or RegExp. Object is a representation of an instance through which we can access numbers. Array is used for representation of similar type of values whereas RegExp is a representation of a regular expression.

Data Type Description
Array represents group of similar values
Object represents instance through which we can access members
RegExp represents regular expression

Each of the data type could be used based on the requirement of coder.

Next ConceptJavaScript Operators