Getting started with JavaScript variables

Source: Internet
Author: User
This article mainly introduces the Getting Started Tutorial on understanding JavaScript variables. it is the basic knowledge in JS getting started. if you need it, you can refer to the following variable as a container for storing information:

x=5; length=66.10;


Do you still remember the algebra I learned at school?


When you recall the algebra courses you have learned at school, you may think of x = 5, y = 6, z = x + y, and so on.

Remember, a letter can save a value (for example, 5), and the above information can be used to calculate that the value of z is 11.

You must have forgotten it, right.

These letters are called variables and can be used to save values (x = 5) or expressions (z = x + y ).


JavaScript variables


Like algebra, JavaScript variables are used to save values or expressions.

You can give the variable a brief name, such as x or a descriptive name, such as length.

JavaScript variables can also save text values, such as carname = "Volvo ".

Rules for JavaScript variable names:


Variables are case sensitive (y and Y are two different variables)


The variable must start with a letter or underscore


Note: JavaScript is case sensitive and variable names are also case sensitive.


Instance


You can change the value of a variable during script execution. You can reference a variable by its name to display or change its value.

This example shows how it works.


Declare (create) JavaScript variables


Variable creation in JavaScript is often called a "declaration" variable.

You can declare JavaScript variables using the var statement:

var x;var carname;


After the preceding declaration, variables have no values, but you can assign values to them when declaring them:

var x=5;var carname="Volvo";


Note: when assigning a text value to a variable, enclose the value with quotation marks.


Assign values to JavaScript variables


Assign values to JavaScript variables using the assignment statement:

x=5;carname="Volvo";


The variable name is on the left of the = symbol, and the value to be assigned to the variable is on the right of the = symbol.

After the preceding statement is executed, the value saved in variable x is 5, and the carname value is Volvo.
Assign values to undeclared JavaScript variables


If the variable you assign has not been declared, the variable is automatically declared.

These statements:

x=5;carname="Volvo";


The effects of these statements are the same:

var x=5;var carname="Volvo";


Redeclare JavaScript variables


If you declare a JavaScript variable again, the variable will not lose its original value.

var x=5;var x;


After the preceding statement is executed, the value of variable x is still 5. When the variable is declared again, the value of x is not reset or cleared.


JavaScript arithmetic


Like algebra, you can use JavaScript variables for arithmetic:

y=x-5;z=y+5; 

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.