JavaScript Datatypes, Operators & Variables

Data Types

Declaring the data type in JavaScript is not essential for the variables that we create. But the others are few similar with C language.

Genrally in JavaScript there are seven data types in which there are three primary data types, two composite data types and two special data types.

The primary data types are: string, number and boolean

The composite data types are: object, array

The special data types are: null, undefined


In JavaScript the value of a variable is either string or number and when we use the variable, we refer to the data it represents.

We use the variables to store, retrieve and manipulate values that appear in our code.

Declaring variables

To keep the multiple values in temporary memory to use in different location in a program it is must to declare the variable.

We should declare variables before using them. We do this using the var keyword.

var count;   // A single declaration.  
var count, amount, level;   // Multiple declarations with a single var keyword.  
var count = 0, amount = 100;  // Variable declaration and initialization in one statement.

If we do not initialize our variable in the var statement, it automatically takes on the value undefined.

Naming Variable

JavaScript is a case-sensitive language. Then a variable name such as theCategory is different from the variable name TheCategory. Variable names can be of any length. The rules for creating variable names are as follows:

Reserved Keywords

break default function return var
case delete if switch void
catch do in this while
const else instanceof throw with
continue finally let try
debugger for new typeof

Here are some examples of valid variable names:

_pagecount , Part9 , Number_Items
// Cannot begin with a number like 37pages
// The ampersand (&) character is not a valid character for variable names. Alpha&Beta

When we want to declare a variable and initialize it, but do not want to give it any particular value, assign it the value null. Here is an example.

        var distance = null;  
        var far = 3 * distance; // far has the value 0.  

If you declare a variable without assigning a value to it, it has the value undefined. Here is an example.

var amount;  // amount has the value NaN because amount is undefined.  
var totalAmount = 3 * amount;

The null value behaves like the number 0, while undefined behaves like the special value NaN (Not a Number). In comparison a null value and an undefined value are equal.


JavaScript is a loosely typed language. It means JavaScript variables have no predetermined type.

In JavaScript, we can write operations on values of different types without causing an exception. The JavaScript interpreter implicitly converts, or coerces, one of the data types to that of the other, then performs the operation.

The rules for coercion of string, number, and Boolean values in JavaScript are the following:

In the following example, a number added to a string results in a string.

var x =500;  
var z = "Years";    
tot = x + z;  // The number is coerced to a string.

In JavaScript, variable names can start witha letter or an underscore(_). The remaining characters can include numbers.

	var fname = "Arindam";
	var lname = "Sarkar";

	document.write("Hello " + fname + " " + lname);

Hello Arindam Sarkar

In the above example, the variables fname and lname are decalred to assign the values. The name stored is displayed write() method. The message "Hello" is concatenated with the variable fname and fname.


The operators supported by JavaScript are Arithmetic, Assignment, Logical, Comparison and conditional operators. Basic Arithmetic operators are +,-,* and /. Apart from these, 3 unary operators and the modulus(%) operator are available in this scripting language. The unary operators are ++(increment), --(decrement) and -(unary minus or negation operator)

The operators in JavaScript


Assigns right operand to left opernd
Subtructs right operand from left operand and assigns the result to the left operand
Multiplies two operands and stores the result in left operand
Divide the left operand by right and assigns the result to the left operand
Divides left operand by right and assigns the remainder to the left operand
Returns True when both the operands are True.
Returns true when either operand is True
Returns True if operand is False and True if operand is True
Returns true if operands are equal
Returns True if operands are not equal
Returns True if left operand is greater than right operand
Returns True if left operand is greater than or equal to right operand
Returns true if left operand is less than or equal to right operand

Similar to the ternary operator in 'C' language JavaScript evaluates conditional expressions using the ternary operator In the following expression it assigns the value 7 to x, if x > y and assign the value 5 to x, if x < y

(x > y) ? x=10 : x=5