Objective:
Interviewer question: What are the data types of javascript?
Because the usual use of time are var xx = xxx; So was asked in amazement, today deliberately understand a bit, there are really some data types.
A string (string), a number, a Boolean (Boolean), an array, an object (objects), null (NULL), undefined (Undefined).
JavaScript has a dynamic type
JavaScript has a dynamic type. This means that the same variables can be used for different types:
Instance
var x; X is undefined
var x = 5; Now X is the number
var x = "John"; Now 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:
Instance
var carname= "Volvo XC60";
var carname= ' Volvo XC60 ';
You can use quotation marks in a string, as long as you do not match the quotes enclosing the string:
Instance
var answer= "It ' s Alright";
var answer= "He is called ' Johnny '";
var answer= ' He is called ' Johnny ';
Try it?
You'll learn more about strings in the Advanced section of this tutorial.
JavaScript numbers
JavaScript has only one numeric type. Numbers can be with decimal points or without:
Instance
var x1=34.00; Use a 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:
Instance
var y=123e5; 12300000
var z=123e-5; 0.00123
Try it?
You'll learn more about numbers in the Advanced section of this tutorial.
JavaScript Boolean
Boolean (logic) can have only two values: TRUE or FALSE.
var x=true;
var Y=false;
Boolean is commonly used in conditional tests. You'll learn more about conditional testing in later chapters of this tutorial.
JavaScript arrays
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):
Instance
var cars=["Saab", "Volvo", "BMW"];
Try it?
The array subscript is zero based, so the first item is [0], the second one is [1], and so on.
You'll learn more about arrays in the chapters later in this tutorial.
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: "John", LastName: "Doe", 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: "John",
LastName: "Doe",
id:5566
};
Object properties are addressed in two ways:
Instance
Name=person.lastname;
name=person["LastName"];
Try it?
You'll learn more about objects in the chapters later in this tutorial.
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.
Instance
Cars=null;
Person=null;
Try it?
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.
Reprinted from: Rookie Tutorial-javascript data types
JavaScript Data type