String, number, Boolean, array, object, Null, Undefined
JavaScript has a dynamic type
JavaScript has a dynamic type. This means that the same variables can be used for different types:
Example
var x//X is Undefinedvar x = 6; X is the number var x = "Bill"; X is a string
JavaScript String
A string is a variable that stores characters, such as Bill Gates.
The string can be any text in quotation marks. You can use single or double quotation marks:
Example
var carname= "Bill Gates"; var carname= ' Bill Gates ';
You can use quotation marks in a string, as long as you do not match the quotes enclosing the string:
Example
var answer= "Nice to meet you!"; var answer= "He is called ' Bill '", var answer= ' He is called "Bill";
JavaScript Numbers
JavaScript has only one numeric type. Numbers can be with decimal points or without:
Example
var x1=34.00; Use the decimal point to write Var x2=34; Do not use the decimal point to write
Large or small numbers can be written by means of scientific (exponential) notation:
Example
var y=123e5; 12300000var z=123e-5; 0.00123
JavaScript Boolean
Boolean (logic) can have only two values: TRUE or FALSE.
var X=truevar y=false
JavaScript Arrays
The following code creates an array named cars:
var cars=new Array (); cars[0]= "Audi"; cars[1]= "BMW"; cars[2]= "Volvo";
or (condensed array):
var cars=new Array ("Audi", "BMW", "Volvo");
or (literal array):
Example
var cars=["Audi", "BMW", "Volvo";
The array subscript is zero based, so the first item is [0], the second one is [1], and so on.
JavaScript Objects
Objects are separated by curly braces. Inside the parentheses, the properties of the object are defined in the form of name and value pairs (name:value). Attributes are separated by commas:
var person={firstname: "Bill", LastName: "Gates", id:5566};
The object (person) in the example above has three attributes: FirstName, LastName, and ID.
Spaces and lines do not matter. Declarations can span multiple lines:
var person={firstname: "Bill", LastName: "Gates", id:5566};
Object properties are addressed in two ways:
Example
name=person.lastname;name=person["LastName"];
Undefined and Null
Undefined This value indicates that the variable does not contain a value.
You can empty a variable by setting the value of the variable to null.
Example
Cars=null;person=null;
declaring variable types
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;
JavaScript variables are objects. When you declare a variable, a new object is created.
JavaScript Data types