JavaScript Learning Three

Source: Internet
Author: User
Tags arithmetic arithmetic operators logical operators ming

imooc.com JS

Give the variable a name (variable name)

In order to differentiate boxes, we can use names such as Box1,box2 to represent different boxes, BOX1 is the name of the box (that is, the name of the variable).

Let's get a good name for the variable! The variable name can be arbitrarily taken, but the name to follow some rules:

1. You must start with a letter, underscore, or dollar sign, followed by letters, underscores, dollar signs, and numbers. As follows:

Correct:               mysum                _mychar             $numa 1          
Error:  6num  //Can not start with  the number%sum//start cannot be used except for (_ $) special symbol, such as (%  +/etc)  Sum+num//And the beginning of the middle cannot use except (_ $) special symbol, such as (%  +/etc )

2. Variable names are case-sensitive, such as: A and A are two different variables.

3. Do not allow variable names using JavaScript keywords and reserved words.

//=======================================

Determine your presence (variable declaration)

We want to use boxes to put things, is not first to find the box, that in programming, the process is called the Declaration of variables, find the action of the box, how to say:

Declaring variable syntax: var variable name;    

var is the equivalent of looking for a box, which is a keyword (that is, a reserved word) in JavaScript, which is a keyword that declares a variable and prepares a "variable" for a position (that is, memory).

var mynum; Declare a variable Mynum

Of course, we can find a box at a time, we can find more than one box at a time, so VAR can also declare more than one variable at a time, separated by a "," comma.

var num1,mun2; Declare a variable NUM1

Note: Variables can also not be declared, used directly, but for specification, need to be declared before use.

//==============================================================================

Diversity of Me (variable assignment)

We can think of variables as a box, a box for storing items, and how to store content in variables.

We use "=" the number to store the contents of the variable, looking at the following statement:

var mynum = 5; Declares a variable mynum and assigns a value.

How do you pronounce this statement? Assigns a value of 5 to the variable mynum. We can also write this:

var mynum; Declaring variable mynummynum = 5; Assigning values to variable Mynum

Note: "=" the function of this number is to assign a value to a variable, not an equal number.

The box can be loaded with clothes, toys, fruits ... such as In fact, a variable is an omnipotent container, and you can store anything in a variable, such as a value, a string, a Boolean, and so on, for example:

var num1 = 123;       123 is the value var num2 = "123";    "123" is a string var num3=true;    Boolean True (True), False (False)

Where the content stored by the NUM1 variable is numeric, the content stored by the num2 variable is a string, the string needs to be enclosed in a pair of quotation marks "" , and the content stored by the NUM3 variable is a Boolean value (True, false).

//========================================================================================

Express your thoughts (expressions)

Expressions are similar to the definitions in mathematics, expressions are algebraic expressions that have certain values, and use operators to concatenate constants and variables together. An expression can contain constants or variables.

Let's first look at the following JavaScript statements:

There are many ways to express "good-bye" in life, such as: English (Goodbye), Internet language (88), body language (waving wave) and so on. In JavaScript expressions everywhere, so be sure to know what to say and see the following scenarios:

Note: MyChar in a string expression is a variable

Note: num is a variable in a numeric expression

Note: num is a variable in a Boolean expression

//=========================================================================

I have other uses (+ number operator)

An operator is a symbol used to specify a certain action in JavaScript.

(1) operator

Look at the JavaScript code below.

sum = numa + numb;

"="and all of them "+" are operators.

There are many such operators in JavaScript, such as arithmetic operators (+ 、-、 *,/etc.), comparison operators (<, >, >=, <=, etc.), logical operators (&&, | |,!). )。

Note: the "=" operator is an assignment, not equal to.

(2) "+" operator

Arithmetic operators are primarily used to perform work similar to subtraction, where "+" does not just represent addition, but can also connect two strings, for example:

mystring = "Java" + "Script"; MyString the value "JavaScript" this string

//==================================================================================

Since add one, subtract one (+ + and--)

In addition to the arithmetic operators (+ 、-、 *,/), there are two very common operators, self-added one “++” ; “--” Let's start with an example:

Mynum = 10;mynum++; The value of Mynum becomes 11mynum--; Mynum's value goes back to 10.

In the above example, mynum++ the Mynum value on the original basis to increase the 1,mynum--so that mynum on the original basis minus 1, in fact, can also be written as:

Mynum = Mynum + 1;//equals mynum++mynum = mynum-1;//equals mynum--

//==========================================================================

Contest (comparison operator)
We first to do a math problem, math test results, Xiao Ming scored 90 points, the small red Test 95 points, asked who test the score high? A: Because "> 90", so the small red test results high.

The greater than the ">" is the comparison operator, Xiao Red test scores and Xiao Ming exam results are the operand, and is two operands.

That is, two operands are compared by comparison operators, and the values are true (TRUE) and False (false).

In JavaScript, there are many comparison operators, and the meanings of these operators are as follows:


Take a look at the following example:    
var a = 5;//defines a variable, which is assigned a value of 5var B = 9; Define b variable, assign value to 9document.write (A<B); A is less than the value of B? The result is true (true) document.write (a>=b); A value greater than or equal to B? The result is False (false) document.write (A!=B); A is not equal to the value of B? The result is true (true) document.write (a==b); is a equal to the value of B? The result is False (false)
============================================= I am with you (logic and operator)

Mathematics inside the "A>b", in JavaScript also expressed as a>b; mathematics "B is greater than a A, a small less than C" is "a<b<c", then in JavaScript can be used in &&, as follows:

B>a && b<c    //"&&" is and means, reading "B is greater than a" and "B is less than C"

As we participate in the college entrance examination, before entering the examination room, must produce the admission certificate and the ID card, both indispensable, otherwise can not take the examination, said:

if (with admission ticket && ID) {   for examination of the examination)

"&&" is the logical AND operator, only "&&" both sides of the value are satisfied (and true), the entire expression value is true.

Logical AND Operator Values table:

Note: If A is false, a && B is false and will not be executed in B; Conversely, if a is true, the value of a && B is determined by the value of B.

//==============================================================

I or you can either (logical OR operator)

"||"The logic or operator, equivalent to the "or" in life, when any one of the two conditions satisfies, the result of "logical OR" is "true".

For example: We plan to travel this week, but we work from Monday to Friday, so it is possible to go in Saturday or Sunday. In two days, if you have a day, you can travel.

var a=3;var b=5;var C;c=b>a | | a>b;  B>a is True,a>b is False,c is true

Logical OR Operator Values table:

Note: If A is true, a | | B is true and will not be executed in B; Conversely, if a is false, it is determined by the value of B to determine a | | The value of B.

//===========================================================================

Topsy (logical non-operator)

"!"is the logical non-operator, that is, the meaning of "no", not true or false, not false is true. Like Xiaohua bought a cup today, Xiao Ming said: "The cup is white", Xiao Liang said: "The cup is red", Xiaohua said: "Xiao Ming said 不是真话 , Xiao Liang said 不是假话 ." Guess what the color of the cup that Xiao Hua bought, the answer: the Red Cup.

Logical non-operator values table:

Look at the following code, what is the value of the variable C:

var A=3;var b=5;var c;c=! (b>a);  The B>a value is true,! (b>a) value is falsec=! (b<a);  The B<a value is False,! (b<a) value is True

//===================================================================================================

Maintain sequencing (operator precedence)

As we all know, the precedence of operators such as division, multiplication is higher than addition and subtraction, for example:

var numa=3;var numb=6jq= Numa + 30/2-numb * 3;  Result is 0

If we want to change the order of operations, we need to add parentheses to change the priority:

var Numa=3;var numb=6jq= (Numa +/(2-NUMB)) * 3; The result is-24.75

Precedence between operators (high-to-low):

Arithmetic operator → comparison operator → logical operator → "=" assignment symbol

If the operation of the sibling is done in the left-to-right order, the multi-level brackets are outward.

var numa=3;var numb=6;jq= Numa + >10 && numb * 3<2;  The result is false

//=====================================================================

JavaScript Learning Three

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.