Detailed JavaScript variables and data types _javascript tips

Source: Internet
Author: User
Tags numeric value javascript array

For a programming language, you must include variables and data types. Today we'll take a look at the variables and data types of JavaScript scripting languages. Relative
In other advanced programming languages such as java,c++, JavaScript is simple.
One, variable
JavaScript variables are loosely typed and loosely used to hold any type of data. A variable is a container for storing information. To define a variable, use the var operator (Var is the keyword) followed by a variable name (the variable name is an identifier). A variable is the amount that can be changed again after initialization.
So let's take a look at the example:

<span style= "FONT-SIZE:18PX;" >var x=2; 
var y=3; 
var z=2+3; 
document.write (x + "<br>"); 
document.write (y + "<br>"); 
document.write (z + "<br>");</span> 

Like algebra: X=2,y=3,z=x+y in Algebra, we use letters (like x) to hold values (such as 2). With the expression z=x+y above, we can calculate that Z has a value of 5. In JavaScript, these letters are called variables. So we can think of variables as containers for storing data.
(1) JavaScript variable name
Like algebra, JavaScript variables can be used to store values (such as x=2) and expressions (such as z=x+y). Variables can use short names (such as x and y) or better descriptive names (such as Age,sum, Totalvolume).
It is to be noted that:

1 variables must begin with a letter
2 variables can also start with the $ and _ symbols (but we do not recommend this)
3 variable names are case sensitive (Y and y are different variables)
(2) JavaScript data type
JavaScript variables can also hold other data types, such as text values (name= "Bill Gates"). In JavaScript, like "Bill Gates"
A text is called a string. There are many types of JavaScript variables, but now we focus on numbers and strings only. Assigning text values to variables
, enclose the value in double or single quotes. Do not use quotes when you assign a value to a variable as a number. If you enclose a value in quotation marks, the value is
Processed as text. A detailed description of the data type is available later.
Instance:

<span style= "FONT-SIZE:18PX;" >var pi=3.14; 
var name= "Bill Gates"; 
var answer= ' Yes I am! '; 
document.write (pi + "<br>"); 
document.write (name + "<br>"); 
document.write (answer + "<br>");</span> 

(3) declaring (creating) JavaScript variables
       Creating a variable in JavaScript is often referred to as a "declaration" variable. A good programming practice is to make the uniform sound
Clear to the desired variables at the beginning of the code. You can also use Var when declaring a variable, but this is not recommended.
       we use var keywords to declare variables: VAR carname;
The variable is empty after the declaration of the        variable (it has no value). If you want to assign a value to a variable, use the equal sign: carname= "Volvo";
      However, you can also assign values to variables when declaring them: Var carname= "Volvo";
      Example: We created a variable named Carname and assigned it to "Volvo" and put it in the HTML section of Id= "demo."

<span style= "FONT-SIZE:18PX;" ><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 
 
 

Click Effect:

(4) A statement, multiple variables
You can declare many variables in a single statement. The statement starts with Var and uses commas to separate the variables:

var name= "Gates", age=56, job= "CEO"; 

Declarations can also span multiple lines:

<span style= "FONT-SIZE:18PX;" >var name= "Gates", 
age=56, 
job= "CEO";</span> 

In computer programs, variables that are not valued are often declared. A variable that is not declared with a value, and its value is actually undefined. The following statement has been executed
The value of the variable carname will be Undefined:var carname;
(5) Re-declaring JavaScript variables
If you re declare a JavaScript variable, the value of the variable is not lost: after the following two statements are executed, the value of the variable carname remains "Volvo":

<span style= "FONT-SIZE:18PX;" >var carname= "Volvo"; 

(6) JavaScript counts
You can do arithmetic with JavaScript variables, using the and + operators like this:
Example:

<span style= "FONT-SIZE:18PX;" ><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 
 
 

Click Effect:

Ii. Types of data
The data types of JavaScript include strings, numbers, booleans, arrays, objects, Null, and Undefined. Before we talk about data types, let's start with an operator typeof.
typeof operator
The typeof operator is used to detect the data type of a variable. Using the typeof operator for a value or variable returns the following string:

<span style= "FONT-SIZE:18PX;" >var box= ' 中文版 '; 
Alert (typeof box); 

Both of these methods are feasible.
The typeof operator can manipulate variables or manipulate literal quantities. Although it can be used in this way, typeof (Box), however, TypeOf is an operator rather than a built-in function. A function is an object, not a data type, so it is necessary to use TypeOf to distinguish between function and object.
The return value is an example of a function:

<span style= "FONT-SIZE:18PX;" >function box () { 
} 

(1) JavaScript has dynamic types
JavaScript has a dynamic type. This means that the same variable can be used as a different type:
Instance:

<span style= "FONT-SIZE:18PX;" >var x//x for undefined 
var x = 6;//x for number 
var x = "Bill";//x for string </span> 

(2) JavaScript string String type
A string is a variable that stores characters. A string can be any text in quotation marks. You can use single or double quotes:;
Instance: Quotation marks can be used in strings, as long as they do not match the enclosing string's quotation marks

<span style= "FONT-SIZE:18PX;" >var carname1= "Bill Gates"; 
var carname2= ' Bill Gates '; 
var answer1= "Nice to meet you!"; 
var answer2= "He's called ' Bill '"; 
var answer3= ' He called "Bill"; 
document.write (carname1 + "<br>") 
document.write (carname2 + "<br>") 
document.write (Answer1 + " <br> ") 
document.write (answer2 +" <br> ") 
document.write (Answer3 +" <br> ") </span> 

The string type also defines an escape character:

(3) JavaScript numbers
JavaScript has only one numeric type. Numbers can be brought in small points or without. The number type contains two types of values: integer and floating-point. The output formats are output in decimal digits. The most basic numeric literal is decimal. Also includes octal numeric literal, leading must be 0, octal sequence (0 to 7, in 8, the hexadecimal literal must be 0x, followed by (0 to 9 and a through f), and floating-point type, that is, a decimal point must be included in the numeric value, and at least one digit must be followed.
1 for those that are too large or too small, we can use the scientific notation (e notation) to represent the exponent power of the first 10 of the value in E. For example:

Copy Code code as follows:
<span style= "FONT-SIZE:18PX;" ><span style= "FONT-SIZE:18PX;" >var box=4.12e-9;</span></span>

2 to determine whether a value exceeds the specified range, you can use the Isfinite () function and, if not exceeded, return true, exceeding the return false.
The 3isNaN () function is used to determine whether this value is Nan. After receiving a value, the isNaN () function attempts to convert the value to a value.
The isNaN () function also applies to objects. During the call to the isNaN () function, the value () method is first invoked, and then determines whether the return value can be converted to a numeric value. If not, then the ToString () method is invoked based on this return value, and then the return value is tested.
Instance:

<span style= "FONT-SIZE:18PX;" >var x1=36.00; 
var x2=36; 
var y=123e5; 
var z=123e-5; 
document.write (x1 + "<br/>") 
document.write (x2 + "<br/>") 
document.write (y + "<br/>" 
) document.write (z + "<br/>") </span> 
 (4) JavaScript Boolean
 Boolean (logic) can only have two values: TRUE or FALSE. For example:
var x=true; 
var y=false; 

(4) JavaScript array
The array subscript is zero-based, so the first item is [0], the second is [1], and so on. The following code creates an array named cars:

<span style= "FONT-SIZE:18PX;" >var cars=new Array (); 
Cars[0]= "Audi"; 
Cars[1]= "BMW"; 
cars[2]= "Volvo";</span> 
 or:
<span style= "FONT-SIZE:18PX;" >var cars=new Array ("Audi", "BMW", "Volvo"); </span> 

Instance

<span style= "FONT-SIZE:18PX;" >var i; 
var cars = new Array (); 
Cars[0] = "Audi"; 
CARS[1] = "BMW"; 
CARS[2] = "Volvo"; 
for (i=0;i<cars.length;i++) 
{ 
document.write (Cars[i] + "<br>"); 
} </span> 

The results of the output are easily known.
(5) JavaScript Object
objects are delimited by curly braces. Within parentheses, the object's properties are defined in the form of name and value pairs (name:value). Properties 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 folding lines are irrelevant. Declarations can span multiple lines:

var person={ 
FirstName: "Bill", 
LastName: "Gates", 
id:5566 
}; 

There are two ways to address object properties:
Instance

var person={ 
FirstName: "Bill", 
LastName: "Gates", 
id:5566 
}; 
document.write (person.lastname + "<br/>"); 
document.write (person["LastName"] + "<br/>"); 

(6) 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.
undefined type

var box; 
Alert (typeof box);//box is the undefined type, the value is undefined, and the string returned by the type is undefined. 

NULL type

var box=null; 
Alert (typeof box);//box is a null type, the value is null, and the string returned by the type is object.

(7) Declaring variable types
JavaScript variables are objects. When you declare a variable, a new object is created. When declaring 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; 

The above is about JavaScript variables and data types of all the content of the introduction, I hope to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.