May 06, 2021 JavaScript
2. JavaScript has a dynamic type
12.. Declare the variable type
String, Number, Boolean, Array, Object, Null, Undefined.
JavaScript has a dynamic type. This means that the same variables can be used as different types:
A string is a variable that stores characters, such as "Bill Gates."
Strings can be any text in quotation marks. You can use single or double quotes:
You can use quotation marks in strings as long as they do not match the quotation marks that surround the string:
You'll learn more about strings in the advanced part of this tutorial.
JavaScript has only one type of number.
Numbers can be taken with or without:
Large or very small numbers can be written by scientific (exponential) counting:
You'll learn more about numbers in the advanced part of this tutorial.
Boolean (logic) can only have two values: true or false.
Boolean is commonly used in conditional testing. You'll learn more about conditional testing later in this tutorial.
The following code creates an array named cars:
Or (the resulted array):
Or (literal array):
The array undersrage is zero-based, so the first item is .0, the second is .1, and so on.
You'll learn more about arrays later in this tutorial.
Objects are separated by parentheses. I nside the parenthesis, the properties of the object are defined as a name and value pair (name: value). The property is separated by a comma:
The object (person) in the example above has three properties: firstname, lastname, and id.
Spaces and folds don't matter. Declarations can span multiple lines:
Object properties are addressed in two ways:
You'll learn more about objects later in this tutorial.
The value of Undefined indicates that the variable does not contain a value.
You can empty a variable by setting its value to null.
When you declare a new variable, you can use the keyword "new" to declare its type:
JavaScript variables are objects. When you declare a variable, a new object is created. |
Tip: JavaScript has an implicit global concept, which means that any variable you don't declare becomes a global object property.
JavaScript Standard Reference Tutorial: JavaScript Data Type