Data Types in JavaScript
JavaScript use data types to identify the type of data that store inside a variable. As JavaScript is a dynamically typed language it infers data types at run time. Dynamic type means the same variable can be used to hold different data types. So, JavaScript allows to store different type values to a variable while running.
Basically, JavaScript supports three type of data types.
- Primitive type
- Composite type
- Trivial type
Primitive Data Type
Primitive data types can be classified into three types.
- Numbers — Number type data type can be either negative or positive values with or without decimal points.
Extra small and extra-large numbers can be written with exponent notation. var x=12e8 //1200000000
var y=12e-5 //0.00012
In JavaScript as other languages there are no data types like integer, short, long. Each and every number always stored as double precision floating point numbers. This format use 64 bits to store a number.
2. Strings — A character or sequence that inside the double quotes or single quotes is become a string. If we want to include single quote in the string we should use backslash(\) to escape the single quote.
To find the length of a string in JavaScript there is an in-build property called length.
var text=“SriLankaJapanIndiaCanadaChinaAmerica”;
var len_of_text=text.length;
3. Boolean — This data type is used to conditional based programming. There are two values as true or false.
By using Boolean() function can check whether an expression is true or false.
Boolean(3>1) //true
In addition easily check true or false.
(3>1) //true
In JavaScript the everything with a value is consider as true.
var b1= Boolean(100); //true
var b2=Boolean(2.4); //true
var b3=Boolean(“school”); //true
var b4=Boolean(‘false’); //true
Everything without a value is false.
var b1=0; //false
var b2=“”; //false
var b3; //false
var b4=null; //false
var b5=false; //false
Composite Data Type
Composite data types can be classified into three types.
- Objects — An object is a property that use to store various types of data types like strings, numbers, Booleans, arrays, functions and other objects. The values are written as name:value pairs. The name:value pairs called as properties.
The objects can be access using two methods.
objectName[“propertyNmae”]
objectName.propertyName
When a JavaScript variable is declared with the keyword ‘new’ it create as an object.
var a= new Number(); //declare as number object
var b= new String(); //declare as string object
2. Arrays — The array data type is used to store multiple data of same datatype and use square brackets([]). The elements in the array can be accessed by index that start from 0.
In objects values can be accessed by using named indexes. But in arrays values should access using numbered indexes.
The full array can access by array name.
In a created array you can change values by using indexes.
cars[1]=“vezel”;
JavaScript array is a kind of object. So, when checking type of a array using typeof property it returns type as object.
3. functions — In JavaScript function consider as a variable type which can be assigned to a variable and it is a block of code created to perform particular task. JavaScript function can be defined using function keyword. The function can be called at anywhere in the program.
A function can have one or more parameters. As JavaScript is dynamic type scripting language parameter can have value of any data type.
var concat(l1,l2){
alert(l1+“ ”+l2);
};
concat(“Sri”,”Lanka”);
concat(3,5);
As above example when calling we can use different data types for parameters.
JavaScript allows to define functions without any name. This unnamed function called anonymous function.
var showMessage = function (){
alert(“Hello World!”);
};
Trivial Data Type
Trivial data types can be classified into two types.
- Undefined — Undefined value means lack of value. When a variable is decaled and if it is not assigned any value it is called undefined data type. JavaScript creates this data type run time. In JavaScript it is a valid data type and can use for doing comparisons.
2. Null — Null data type means the no value or absence of a value. If a variable currently have no value we can simply assign null. It is not consider as undefined or zero value. It is a data type that defined but contains no value.
By using typeof operator able to check the data type of variable that use in JavaScript.
typeof 3.45 //return number
tyoeof “Sri Lanka” //return string
typeof true //return boolean
typeof {name:‘Saman’, age:34} //return object
typeof [1,2,3,4] //return object
typeof function myFunc(){} //return function
typeof x //return undefined
typeof null //return object