JavaScript Variables and Data Types

Variables are named memory locations that can hold different types of values. Whereas data type is a classification of data which tells the compiler or interpreter how the programmer intends to use the data. In javascript, we have 3 types of datatype –

  • Primitive
  • Non-Primitive
  • Special
JavaScript Data Types Diagram
JavaScript Data Types Diagram

Primitive

A primitive type is the basic type of data type by which we can create Non Primitive type variables. It’s defined as immutable values.

Boolean true or false
String text
Number 007 or 0.07

Non-Primitive

Non-primitive types are the combination of primitive types of variables.

Array [1, 2, 3]
Date Mon Jan 02 2017 11:55:56 GMT+0530 (IST)
Function function () {}
Object { text: “text” }
RegExp /text/

Special

Special types are some special value. Like –

Boolean undefined
Null null

How to use the variable inside javascript?

In Javascript we use the keyword variable_name = value; this formula for creating a variable. Ex-


var isTure = true; // for boolean
var location = “India”; // for string
var age = 23; // for number
var isUndefined; // for undefined
var isNull = null; // for null
var obj = { isTrue: true }; // for object
var arr = [1, 2, 3]; // for array
var date = new Date(); // for date
var reg = /test/; // for regex
var fun = function () {}; // for function

Be careful when you are using null and undefined values. So the difference between null and undefined is –
If we have a property without value or definition, is undefined. And Null is a special value meaning “no value”. Null is an object type of value whereas undefined is the undefined type itself.

JavaScript Identifiers

A variable should be identified with unique names. Unique names are called identifiers. It is a good practice to create a more descriptive variable (name, age). Some basic rules to create variables –

  • Variable can contain letters, digits, underscores, and dollar signs.
  • Variable can Start with letter or $ and _.
  • Variable should be case sensitive.
  • Variable cannot use reserved words.

For more details – w3school

About the Author: Pankaj Bisht