Variable types in JavaScript

There are four types of variables in JavaScript: number, string, Boolean, and null.

Number variables

JavaScript does not require numbers to be declared as type of numbers (integer, decimal, etc). Numbers are just numbers!

Examples:

var motorbikeprice = 3420;
var phoneprice = 89.99;
var spendingprice = -250.67;
var savingprice = 0;
var houseprice = 2.50e5;

Notice letter e and the number 5 that follows it in the value of the variable houseprice. Number 5 is the exponent of e where e is 10. It translates into 2.50 × 105.

String variables

String variables are variables that represent a string of text. The string of text, that represents the value of the variable, must be placed within quotation marks (single or double quotation marks are allowed) and it can contain letters, numbers, spaces or almost anything you like.

Examples:

var mycomputer = "processor i5, 8GB of RAM";
var mymotorbike = "Honda PCX";
var mydog = "Flora";

Note:
JavaScript strings are case sensitive.
A JavaScript string must be opened and closed with the same type of quotation marks.

Boolean variables

A Boolean variable is a JavaScript variable which has a value of true or false.

var DanWebDeveloper = true;
var DanHistorian = false;

Notice that the values true and false are not placed between quotation marks. That is because they are recognized as Boolean values. Instead of using true or false you can use 1 for true and 0 for false.

var DanWebDeveloper = 1;
var DanHistorian = 0;

Null variables

A null variable is a variable that has no value.

var DanHistorian = null;

As Boolean variables, the value null of a null variable does not need to be enclosed within quotation marks since is is recognized as a JavaScript keyword with a predefined value of nothing. Null variables come in handy when you test for input in scripts.

Leave a Reply