A data type or simply type is an attribute of data which tells the interpreter how the programmer intends to use the data.
There are two types of data types in JavaScript. Data Types
- Primitive data type
- Non-primitive (reference) data type
Primitive Data Types:
- String: Sequence of characters used for text.
- Number: Decimals & Integers. Are basically floating point.
- Boolean: Logical True or False.
- Undefined: Data type of undefined variables.
- Null: Non-existent.
In the below code snippet a variable is declared, given a name and is assigned with a value. var keyword is nothing but variable, fName is variable name i.e. identifier, = is assignment operator and ‘Surya is a string value. Note: A string value is enclosed in single quotes or double-quotes. typeof is an inbuilt function which returns the data type of a variable. Here it is String.
var fName = 'Surya'; //Data Type : String
console.log(fiName); // Surya
console.log(typeof fName); //String
In the below example variable age is a Number.
var age = '28'; //Data Type : Number
console.log(age); // 28
console.log(typeof age); //Number
Some other examples of datatypes:
var lastName = "Singam";
var age = 28 ;
console.log(typeof age); // Number
var married = false;
console.log(typeof married); // Boolean
var palceOfBirth;
console.log(typeof palceOfBirth); // Undefined
Undefined:
If a variable is declared but the value is not assigned, then the value of that variable will be undefined
. For example,
let name;
console.log(name); // undefined
NULL:
In JavaScript, null
is a special value that represents an empty or unknown value. For example,
let number = null;
var typeof throws an error as it is a keyword and can not be used as a identifier.
var typeof = ; //Throws an error One can not use keywords
//(predefined) names for variable identifiers.
string concatinated:
Concatenation is the process of appending one string to the end of another string.
var num1 = 10;
var num2 = '20';
var result = num1+num2;
console.log(result); // 1020
2. Non-primitive (reference) data type:
Types of non-primitive data types in JavaScript:
Data Type | Explanation |
---|---|
Object | It is an instance that allows us to access members in JavaScript. |
Array | It represents a collection of similar elements in JavaScript. |
RegExp | It represents a regular expression in JavaScript. |
- Object: In Javascript, an object is a thing defined using attributes and methods. In Javascript, everything is an object.
Learn more about Objects
- Array: We can store several elements having similar characteristics under a single name by using an array.
Learn more about Arrays