Value types: String, Number, Boolean, Null, Undefined, Symbol.
Reference data types: Object, Array, Function.
Note: Symbol is a new primitive data type introduced in ES6 that represents unique values.
JavaScript has dynamic typing. This means that the same variable can be used for different types:
The instance
var x; / / x is undefined
var x = 5; // Now x is a number
var x = “John”; // now x is a string
JavaScript string
Strings are variables that store characters such as “Bill Gates”.
The string can be any text in quotes. You can use single or double quotation marks:
The instance
var carname=”Volvo XC60″;
var carname=’Volvo XC60′;
You can use quotation marks in strings as long as they do not match the quotation marks surrounding the string:
The instance
var answer=”It’s alright”;
var answer=”He is called ‘Johnny'”;
var answer=’He is called “Johnny”‘;
JavaScript digital
JavaScript has only one numeric type. Numbers may be numbered with or without a decimal point:
Instance var x1 = 34.00; // Use a decimal point
var x2=34; // Do not use the decimal point to write
The greatest or smallest numbers can be written by scientific (exponential) notation:
The instance
var y=123e5; / / 12300000
var z=123e-5; / / 0.00123
JavaScript Boolean
Boolean (logic) can have only two values: true or false
var x=true;
var y=false;
Booleans are often used in conditional testing. You will learn more about conditional testing later in this tutorial.
JavaScript array
The following code creates an array named cars:
var cars=new Array();
cars[0]=”Saab”;
cars[1]=”Volvo”;
cars[2]=”BMW”;
Or (condensed array) :
var cars=new Array(“Saab”,”Volvo”,”BMW”);
Or (literal array) :
Array subscripts are zero-based, so the first item is [0], the second is [1], and so on.
JavaScript object
Objects are separated by curly braces. Inside parentheses, the attributes of the object are defined as name and value pairs. Properties are separated by commas:
var person={firstname:”John”, lastname:”Doe”, id:5566};
The object (person) in the above example has three attributes: FirstName, lastName, and ID.
Spaces and folds don’t matter. Statements can span multiple lines:
var person={
firstname : "John",
lastname : "Doe",
id : 5566
Copy the code
};
Object attributes can be addressed in two ways:
Undefined and Null
Undefined Indicates that the variable has no value.
You can empty a variable by setting its value to NULL.
The instance
cars=null;
person=null;
When you declare a new variable, you can use the keyword “new” to declare its type:
var carname=new String;
var x= new Number;
var y= new Boolean;
var cars= new Array;
var person= new Object;
Lamp JavaScript variables are all objects. When you declare a variable, you create a new object.