You are on page 1of 4

JavaScript Data Types

 There are a few different data types in

JavaScript: numbers, strings, booleans, objects, and arrays.

 JavaScript data types are an important concept in programming. Lots of programming

languages use classification for a better coding experience and bug-free code.

 For the program to know how to treat a variable, it must know its data type.

 Data type can be identified using JavaScript typeOf operator.

Example:
var year = 2016; // Number
var firstName = "Wilson"; // String
var person = {name:"Wilson", age:"old"}; // Object
var person = ["Wilson", "old"]; //array
var x=null; // null
var x; //undefined

 Strings

Strings are JavaScript data types that hold information in text. They are encapsulated with

quotes (unlike numbers, which do not need them). Both double and single quotes are

accepted.

var car = "Audi 80"; // double quotes

var car = 'Audi 80'; // single quotes

var text = "It's sunny outside"; // Single quote inside double quotes

var text = "It is called 'Audi'"; // Single quotes inside double quotes
var text = 'It is called "Audi"'; // Double quotes inside single quotes

 Numbers

JavaScript numbers can be written with or without decimals. As we already covered, they

should never be written within quotes - otherwise, it will be read as strings.

var x = 4.00; // Written with decimals

var y = 44; // Written without decimals

 Objects

One of JavaScript types of data are objects which are defined with curly braces. The object

type must have a property and a value: they are written in propertyName:value pairs.

The example below creates an object called human that has four properties:

var human = {

firstName: "Bob",

lastName: "White",

age: 44,

eyeColor: "brown"

};

 Booleans

A boolean is a data type that can have only on of two possible values: true or false.

var xyz = true;


var xyz = false;

 Arrays

JavaScript arrays are defined with square brackets. Arrays are one of those JavaScript data

types that are commonly used with variables. Array items are normally separated with

commas.

var cars = ["Audi", "Mazda", "Volvo"];

Note: The first element of the array is always indexed as 0.

undefined and NULL

If a variable does not have a value, JavaScript automatically sets it as undefined. In the

example below, the variable has a name but does not have a value assigned. Without the

value, we do not know what is the type of this variable, which makes it undefined. If you use

JavaScript typeOf operator on an undefined JavaScript variable, the program will

return undefined only:

Example

var human; // Value is undefined, type is undefined

JavaScript null also means nothing. The difference is that even though it has no value, it

is an object. You can clear an object by setting it to JavaScript null. This also means that

using typeOf JavaScript operator will identify this variable as an object:

Example

var human = null; // Value is null, but type is still an object

NOTE:
 There are a few JavaScript types of data, which serve a different purpose in the

code.

 It is essential to use the correct syntax for data types so the browser understands

your code.

 You can use JavaScript typeOf operator to check what type of JavaScript data

something is.

You might also like