Variables and data types in javascript

data-type

Variables are named memory location that can hold a different type of values. Whereas It 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 –

data-type
Variables and data types in javascript
  • Primitive
  • Non-Primitive
  • Special

Primitive

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

Boolean true or false
String text
Number 007 or 0.07

Non-Primitive

Non-primitive type are the combination of primitive types of variable.

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

Special

Special type are some special value. Like –

Boolean undefined
Null null

How to use variable inside javascript?

In Javascript we use 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 value. 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 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 variable more descriptive name like (name, age). Some basic rule to create variable –

  • 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

You May Also Like

About the Author: Pankaj Bisht