JavaScript Data Types

JavaScript support various data types. javaScript is a dynamic language. It means you don't have to define the type of variables. You just have to use a keyword var to define the data type. There are six basic data types in JavaScript. They are:

  • Numbers
  • String
  • Booleans
  • Objects
  • Functions
  • Undefined Values


Numbers

Data of the number types are the numeric values. Fractional numbers are written by using a dot.

Example:

var num1 = 10;
var num2 = 15.45;

Run Example

For a very big or very small numbers, you can also use scientific notation by adding an e(for "exponent"), followed by the exponent of the number:

Example:

var num1 = 1774e4;
var num2 = 7889e8;

Run Example


Strings

The next basic data type is the String. Strings are used to represent text. They are written by enclosing their content in quotes.

Example:

var str1 = "This is the forst string.";
var str2 = "This is the second string.";

Run Example

String cannot be divided, multiplied or subtracted, but the + operator can be used on them. It does not add, but it glues multiple strings together. The following line will produce the string concatenate.

Example:

var str3 = "con"+"cate"+"nate";

Run Example


Boolean

Often you will need a value that simply distinguishes between two posibilities like yes and no or on and off. For this, JavaScript has a Boolean type, which has just two values: true and false.



Undefined Values

There are two special values, written null and undefined, that are used to denote the absence of a meaningful value. They are themselves values, but they carry no information.
The difference in meaning between null and undefined is an accident of JavaScript's design, and it doesn't matter most of the time. In the cases where you actually have to concern yourself with these values, it is recommended treating them as interchangeable.

Function and object will be described later.