Home » javascript » Datatype in JavaScript

Datatype in JavaScript

JavaScript in Internal File

Like other programming languages, JavaScript also provides 2 different data types to hold different types of values. Here are the two types of data types:

  1. Primitive data type
  2. Non-primitive data type

JavaScript is a dynamic type language, which means the type of the variable doesn’t need to be specified (it is dynamically used by JavaScript engine).

The keyword var is used here to declare a variable.

The datatype is declared when it is initialized with a particular values such as numbers, strings etc. For example:

  1. var a=10000; //holding number  
  2. var b=”JavaScript”;    //holding string  
  3.  var c;           // c is now undefined
  4.  c= 25           // cis a Number    
  5. c = “JavaScript”     //c is string

JavaScript primitive data types

These are the five types of primitive data types in JavaScript:

Data Type Description
String String represents sequence of characters e.g. “JavaScript”
Number Number represents numeric values e.g. 50
Boolean Boolean represents either false or true
Undefined Undefined represents value that can’t be defined like infinity etc
Null Null represents nothing i.e. no value

JavaScript non-primitive data types

The three JavaScript non-primitive data types are as follows:

Data Type Description
Object It represents instance through which we can access members and methods
Array It represents collection of similar data type values

JavaScript String Rules

Quotes can be used inside a string if they don’t match the quotessurrounding the string:

JavaScript evaluates expressions from left to right. Also it mconcatenates numbers with strings in an expression. Few examples are as follows:

DifferenceBetween Undefined and Null

Undefined and null are equal with respect to value but different with respect to type.

The typeof null is object whereas undefined is not an object.

JavaScript Arrays

JavaScript arrays are written with square brackets and the array items are separated by comma.

The following code creates an array called colours and initializes with three colours:

Array index starts with zero, which means the first item is colours[0], second is colours[1], and so on.

JavaScript Objects

JavaScript objects are written with curly braces like the following example below:

Managed by MochaHost - Fast & Easy WordPress Hosting