HTML-JS base variable and input-output operator branching structure

Source: Internet
Author: User
Tags bitwise operators

The variables in JS
1, JS in the wording of the variable declaration;
VAR num=10;//variables declared with VAR are local variables only valid within the current scope
num=10;//variables declared with VAR, default to global variables, are valid throughout the JS file.
2. Declare multiple variables at the same time using a single line of statements. var x=10,y,z=8;
Y belongs to the declared but unassigned status result is undefined.

Considerations for declaring variables
The declaration of all variables in ①js uses the var keyword, which is exactly what data type, depending on the type of assignment to the variable.


The same variable in ②JS can modify the data type of a variable when it is assigned multiple times.


var=10;//Initial declaration, A is an integer type
a= "AAA"///duplicate assignment, the integer type A is modified to the string type.


The ③ variable can be declared with Var, or it can be used without the Var declaration.
The [difference] is declared as a local variable using VAR and not declared as a global variable using var.


④ is only declared with Var, but is not assigned a value. The result is undefined.
For example, Var a;//a is undefined. However, if you do not declare or assign a value of a, the direct use will be an error.


⑤ the same variable name, you can use the Var declaration multiple times, but the subsequent VAR does not have any ova. The second time you use the Var declaration, you will only be understood as
an ordinary assignment operation.
2 . Naming requirements for variable names
① variable names can only consist of letters, numbers, and underscores.
② cannot start with a number.
③ variable names are case-sensitive and uppercase and lowercase letters are different variables.
3 . Naming conventions for variable names
① to conform to the small hump rule (Camel name law):
first letter lowercase, followed by the first letter of each word capitalized
mynameiszhaoyuan√
② value using Hungarian nomenclature, all letters lowercase, and the words are separated by underscores.
my_name_is_zhao_yuan√
Mynameisjianghaox can be used but not standardized.
4,the data type in JS
undefined: not defined. Variables declared with VAR have been used, but no value has been assigned. For example var A;
null: a null reference.
Boolean: Boolean type. means true or false. Only two values are true false.
Number: numeric type. Can be a positive number, or it can be a decimal.
String : type. Use "" or "wrapped content, called a string."
Object : type.
5. [ commonly used numerical functions ]
① IsNaN (); Determines whether a variable or constant is Nan (not a number non-numeric)
when you use isNaN to judge, you try to convert using the number () function. If the final result can be converted to a number,
is not nan and the result is false.
The ② number () function, which attempts to convert other types of data, into a numeric type.
[String type]
The >>> string is a pure numeric string and will be converted to the corresponding number: "111"->111
>>> string is an empty string and will be converted to 0: ""->0;
>>> string contains any other characters and cannot be transferred. "1a"->nan
[Boolean type]
true->1
false->0
[null/undefined]
null->0
Undefined->nan
[Object]
③ parseint () converts a string to an integer type.
>>> Pure numeric string, can go. "->12" "12.9"->12 (when the hour conversion, directly erase the decimal point, not rounding)
>>> empty string cannot go ""->nan
>>> A string containing other characters that will intercept the number part "123a456"->123 "b123a456" before the first non-numeric character->nan
>>>parseint () can only be turned into strings, and other types are all Nan.
[number function differs from parseint ()]
1.number functions can be transferred to various data types, parseint () can only be turned to strings;
2. The results are not exactly the same when you turn the strings.
④parsefloat () converts a string to a numeric type;
The conversion rule is the same as parseint (), except that if there are decimals, the decimal point is preserved. If there are no decimals, it remains an integer.
" 12.5"->12.5 "one"->11
⑤typeof () Detecting the data type of a variable
string->string
Numeric->number
->undefined Not defined
True/false->boolean
Object/null->object
function->function

[input and output statements commonly used in JS ]
1,document.write (); The contents of () are printed and exported to the browser;
Be aware that all content except variable constants must be placed in the. Variables and constants must be placed outside the "".
If you have both variables and strings, you must use the + link


Eg:document.write("Solitaire in the left hand" +left+ "<br/>")
2.alert (); Use pop-up window output;

The contents of the pop-up Window warning, () are the same as the above requirements.


3,promt (); Pop-up window input \ n

Accept two-part parameters:
① the contents of the input box above, optional
② The default information in the input box, optional
Indicates the content of the cue above the input box when only one part is written
You can define variables, receive input, click the OK button, and the variable will be assigned to the input content.
Click the Cancel button and the variable will be assigned a value of NULL.
The data type that is received by default when you enter content is a string.

1. arithmetic Operation (single-mesh operator)
+ Plus, minus, * multiply,/divide,% withdraw, + + self-increment 、--self-reduction
>>> +: There are two kinds of functions, link string/addition operation. When the + sides are all numbers, the addition operation is performed, and when the + sides have either side as a string,
the role of the link string. In addition to +, the rest of the symbol operation will attempt to convert the left and right sides with the number function to the numbers.
>>>/: The result will retain the decimal point.
>>> + +: Add a variable to the original base
----to reduce the variable on the original basis
"The similarities and differences between a++ and ++a" pre-air strikes lifted
① The same point: the value of a is added 1 after playing either a++ or ++a operation
② different points: a++: First with a value operation, then the a+1;
++a: First put the a+1, and then use a value of the operation.

2. Assignment Operation
= Assignment = = = *=/=%=
a +=b; equivalent to A=a+b; but the former is more efficient than the latter. , it is recommended to use the + = notation.

3. Relational Operations
= =, = = =, =,!== not congruent >, <, >=, <=
>>> relational operators, the result after operation, can only be a Boolean type;
>>> to determine if a number is in a certain range, you must use && link
a<=10 && a>=0√
>>>===: Strictly equals. Requires not only the same type, but also the same value. Type different result direct false; type is the same, download for next judgment.
>>>==: Equals. Same type, with = = =
Same, the type is different, first try to use number (), turn both sides into numbers, and then judge.
But there are individual exceptions, such as Null==falsexnull==undefined√

4. conditional operator (multi-mesh operation)
A>b?true:false
made up of two important symbols:? And:
when? When the preceding section evaluates to True, execute: The preceding code
when? When the preceding part evaluates to False, execute: The following code
: Both sides can be numeric values, then the entire equation can be used for assignment. var a=1<2?1:2;
Both sides of the colon can be code blocks, direct code execution, 1<2?alert (1): Alert (2);
The multi-mesh operator can be nested in multiple layers of Var a=1<2?alert (1):(1<2?4:5);

5, bitwise operators, logical operators
&& and, | | Or! Non-
&& Both sides are set, the result is true
| | On either side of the set, the result is true
6. Precedence of Operators
() parentheses highest
! + +--Monocular operator
* / %
+ -
< > <= >=
== !=
&&
| | With or at the same time, with a ratio or higher.
+ = = *=,/= the lowest is a variety of assignments.

"If-else structure"
1, the structure of the wording
if (judging condition) {
when the condition is true, execute if{}
}
else{
when the condition is False, execute else{}
}
2. precautions
①else{} statement block. can be omitted according to the situation.
the curly braces after ②if and else can be omitted, but after the curly braces are omitted, the if and else can follow only one statement.
Therefore, it is not recommended to omit curly braces.
3. If the criteria in parentheses support the data type, supported by the case.
①boolean:true is True, False is false.
②string: Empty string is false. All non-empty strings are true.
③number:0 is false, all non-0 digits are true.
④null undefined NaN: All is false.
⑤object: All is true.
* */

/*
"Multiple If Structure"
1, the structure of the wording
if (condition i) {
When conditions are established, the actions performed
}
else if (condition two) {
condition One does not hold, and condition two establishes the operation that executes
}
else if () {
when all of the above conditions are not true, the operation is performed.
}
2, multiple if structure, each judgment condition is mutually exclusive, can only select one of the road execution, encountered the correct option
and after the execution, directly out of the structure, no longer judge the subsequent branches;

3. Nested IF structure
1, if the wording:
if (condition i) {
//Condition I established
if (condition two)
{
condition I set up && condition II established
}else{
condition One establishment && condition two not established
}
}
else{
//Condition one not established
}
2. In the nested if structure, if the curly brace is omitted, the else structure will always belong to the nearest if structure
3, nested structure can be nested in multiple layers, but generally not more than three layers
It is generally not recommended to use multiple if structures for nested IF.

HTML-JS base variable and input-output operator branching structure

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.