[JavaScript] JavaScript learning six seven JavaScript variables/Operators

Source: Internet
Author: User
Document directory
  • Syntax
  • Example

Variables are containers used to store 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 calledVariableThe variable can be used to save the value (x = 5) or expression (Z = x + y ).

Javascript Variables

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

You can give a short name to the variable, suchXOr more descriptive names, suchLength.

Javascript variables can also save text values, suchCarname = "Volvo".

Rules for JavaScript variable names:
  • Variables are case sensitive (YAndYIs two different variables)
  • The variable must startStart with a letter or underline

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 useVaR statementTo declare JavaScript variables:

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 executedXThe saved value is5, AndCarnameThe value isVolvo.

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; 

In the next section of this tutorial, you will learn the operators that can be used between JavaScript variables.

Javascript comparison and logical operators

Comparison and logical operators are used to test true or false.

Comparison Operators

Comparison operators are used in logical statements to determine whether variables or values are equal.

Given x = 5, the following table explains the comparison operators:

Operator Description Example
= Equal X = 8 is false
=== Full (Value and type) X = 5 is true; X = "5" is false
! = Not equal X! = 8 is true
> Greater X> 8 is false.
< Less X <8 is true
> = Greater than or equal X> = 8 is false
<= Less than or equal X <= 8 is true
How to Use

You can use a comparison operator in a condition statement to compare values and then take actions based on the results:

if (age<18) document.write("Too young");

In the next section of this tutorial, you will learn more about conditional statements.

Logical operators

Logical operators are used to determine the logic between variables or values.

Given x = 6 and Y = 3, the following table describes the logical operators:

Operator Description Example
&& And (X <10 & Y> 1) is true
| Or (X = 5 | Y = 5) False
! Not ! (X = Y) is true
Conditional Operators

Javascript also contains conditional operators that assign values to variables based on certain conditions.

Syntax
variablename=(condition)?value1:value2 
Example
greeting=(visitor=="PRES")?"Dear President ":"Dear ";

If the value in the variable visitor is "pres", the value of "Dear President" is assigned to the variable greeting; otherwise, the value of "dear" is assigned ".

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.