JavaScript use (iii)

Source: Internet
Author: User
Tags arithmetic

1.JavaScript Variables

A variable is a "container" for storing information.

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

1.1 is like algebra.

X=5
Y=6
Z=x+y

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

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

In JavaScript, these letters are called variables.

1.2JavaScript variables

Like algebra, JavaScript variables can be used to hold values (such as x=5) and expressions (such as z=x+y).

Variables can use short names (such as x and y), or they can use better descriptive names (such as age, Sum, totalvolume).

    • Variables must start with a letter
    • Variables can also start with the $ and _ symbols (although 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.

1.3JavaScript Data types

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

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

There are many types of JavaScript variables, but for now, we only focus on numbers and strings.

When you assign a text value to a variable, enclose the value in double or single quotation marks.

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

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

1.4 declaring (creating) JavaScript variables

Creating a variable in JavaScript is often referred to as a "declaration" variable.

We use the VAR keyword to declare variables:

var carname;

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

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

Carname="Volvo";

However, you can also assign a value to a variable when you declare it:

var carname="Volvo";

In the following example, we create a variable named Carname and assign it a value of "Volvo" and put it in an HTML paragraph id= "Demo":

<p id="demo"></p>var carname="Volvo"  ;d Ocument.getelementbyid ("demo"). Innerhtml=carname;

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

1.5 One statement, multiple variables

You can declare many variables in a single statement. The statement starts with VAR and separates the variables with commas:

var lastname="Doe", age=, job="Carpenter" ;

Declarations can also span multiple lines:

var lastname="Doe",age =$, job="  Carpenter";

1.6Value = undefined

In computer programs, variables that are not valued are often declared. A variable that is not declared with a value, whose value is actually undefined.

After the following statement has been executed, the value of the variable carname will be undefined:

var carname;

1.7 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":

var carname="Volvo"var carname;

1.8JavaScript arithmetic

You can use JavaScript variables to do arithmetic, using the operators such as = and +:

y=5; x=y+2;

JavaScript use (iii)

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.