[JavaScript tutorial] JavaScript Variables

Source: Internet
Author: User
A variable is a & quot; Container & quot; used to store information ;.

JavaScript Variables

A variable is a "Container" used to store information ".

Instance

var x=5;var y=6;var z=x+y;

Just like Algebra

X = 5
Y = 6
Z = x + y

In algebra, we use letters (such as x) to save values (such as 5 ).

Using the above expression z = x + y, we can calculate the value of z as 11.

In JavaScript, these letters are called variables.

You can think of variables as containers that store data.

JavaScript Variables

Like algebra, JavaScript variables can be used to store values (such as x = 5) 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 ).

The variable must start with a letter.

Variables can also start with $ and _ (but we do not recommend this)

Variable names are case sensitive (y and Y are different variables)

Both JavaScript statements and JavaScript variables are case sensitive.

JavaScript Data Type

JavaScript variables can also save other data types, such as text values (name = "Bill Gates ").

In JavaScript, a text like "Bill Gates" is called a string.

JavaScript variables have many types, but now we only focus on numbers and strings.

When you assign a text value to a variable, it should be enclosed by double quotation marks or single quotation marks.

Do not use quotation marks when you assign a value to the variable. If you enclose a value in quotation marks, the value is processed as text.

Instance

var pi=3.14;var person="John Doe";var answer='Yes I am!';

Declare (create) JavaScript Variables

Creating a variable in JavaScript is usually called a "Declaration" variable.

We use the var keyword to declare the variable:

var carname;

After the variable is declared, the variable is empty (it has no value ).

To assign a value to a variable, use the equal sign:

carname="Volvo";

However, you can assign values to variables when declaring them:

var carname="Volvo";

In the following example, a variable named carname is created and assigned "Volvo" to it, which is then included in the HTML section of id = "demo:

Instance

var carname="Volvo";document.getElementById("demo").innerHTML=carname;

A good programming habit is to declare the required variables at the beginning of the Code.

One statement and multiple variables

You can declare many variables in a statement. This statement starts with var and uses commas to separate variables:

var lastname="Doe", age=30, job="carpenter";

The statement can also span multiple rows:

var lastname="Doe",age=30,job="carpenter";

Value = undefined

Variables without values are often declared in computer programs. A variable declared without a value. Its value is actually undefined.

After the following statement is executed, the carname value of the variable is undefined:

var carname;

Redeclare JavaScript Variables

If you declare a JavaScript variable again, the value of the variable will not be lost:

After the following two statements are executed, the carname value of the variable is still "Volvo ":

var carname="Volvo"; var carname;

JavaScript count

You can use JavaScript variables to calculate the number. The = and + operators are used:

Instance

y=5;x=y+2;

You will learn more about JavaScript operators later in this tutorial.

The above is the content of the JavaScript variable in the [JavaScript tutorial]. For more information, see the PHP Chinese website (www.php1.cn )!

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.