JS Basics-2

Source: Internet
Author: User

# # #使用变量名的注意事项

# # # #关键字 (reserved word)

Words that have already been defined in JavaScript can no longer be used by the user as variable names

>>>! [] (Images/identifier_04.bmp)
>
> In addition, there are three words that are not reserved words, but because they have special meanings, they should not be used as identifiers: Infinity, NaN, undefined.

# # #变量提升
----
JS Runtime will collect all the variables, and placed in the beginning of the JS position. (The function will also ascend)
# # Identifiers
An identifier is a name used to identify a specific object. The most common identifier is the variable name,
and the name of the function to be mentioned later. The identifier for the JavaScript language is case sensitive, so a and a are two different identifiers.

> Identifiers have a set of naming conventions, which are illegal identifiers that do not conform to the rules. If the JavaScript engine encounters an illegal identifier, it will get an error.
>> Naming conventions:
>-the first character can be any Unicode (universal code) letter, as well as the dollar sign ($) and underscore (_).
>-the second character and subsequent characters, and can also be used as a number.

>> Chinese is a valid identifier and can be used as a variable name (this way * * Not recommended * *)


# # #常量 ES6

A constant is the amount of the program that will not be changed while it is running.

Cases:
Const pi=3.14;


1, constants are block-level scopes, scoped and let.
2, can not be re-assigned value, can not be redefined, will be directly error!
3, at the time of creation, the constant needs to be initialized, that is, assign value, otherwise it will be an error!
4, a constant cannot have the same name as another variable or function in the scope where it is located
5, the value can be any type, but cannot be re-assigned. Object properties are not covered by protection
6, no mandatory canonical naming, in order to differentiate from variables, all uppercase.
Compatibility: IE 11 Google 21

# # #类型之间的转换
# # #隐式转换

1, when different data types are being operated on each other,
2, when a Boolean value is obtained for a non-boolean type of data

When expected to be a number:
Arithmetic operations, our results and the number of operations are numbers, the data will be converted to a number to calculate (-*/%)

| Type | Before conversion | After conversion |
| ------------- |:-------------:| -----:|
| number | 4 | 4 |
---------------------------------------
| | ' 1 ' | 1 |
| string | ' ABC ' | NaN |
| | ' | 0 |
---------------------------------------
| Boolean | true | 1 |
| Boolean | False | 0 |
---------------------------------------
| Undefined | Undefined | NaN |
| null | null | 0 |

When expected as a string:

Converted to a string using the + sign automatically

When expected to be boolean:

Convert to Boolean value, undefined null ' 0 NaN-->false remainder is True

# # #强制类型转换
Convert to numeric number (), parseint (), parsefloat () conversion function

When number is converted, it returns a value of Nan if it cannot be converted to a numeric value, and can return a floating-point integer for conversion.
parseint () When a conversion is performed, a positive integer that cannot be converted to a numeric value is returned and converted individually
Parsefloat () can return a floating-point number alone can only parse the first decimal point

The former converts the value into an integer, which converts the value into a floating-point number.
The string type calls these methods for the functions to run correctly, and if a non-number is encountered, returns Nan not a

Tips:
Convert string a= "" + Data
Convert Boolean!! Data type
Convert numeric data type * or/1; easily misunderstood

# # #JavaScript的运算符
-Arithmetic operations
! [] (Images/ys.png)
>-Addition Operator (addition): X + y
>-Subtraction operator (subtraction): x y
>-Multiplication operator (multiplication): X * y
>-Division Operator (Division): X/Y
>
>-remainder operator (remainder): x% y
>-Self-increment operator (Increment): ++x or X + +
>-Auto-decrement operator (decrement):--x or x--
>-negation operator (Negate):-X


-Assignment operation

-Comparison operation
Character encoding: All numbers, letters, and other symbols in a computer are numbered. This facilitates data transfer of charCodeAt ()
If it is more than one character, it is compared individually. (Unicode encoding of characters.) This return value is an integer between 0-65535)


-Logical operation


Logical non, can be used for any value. The first is converted to a Boolean value, then the inverse (also called the inverse operator)
Expected Boolean value: undefined null ' 0 NaN-->false remainder is True

Logic with, there are more than two operands, judging whether all two of them will return true

Logical OR, two or more operands, judging if there is a return true of two

Logic and, logically, are not also known as short-circuit operators.
Cases:
var b_flag=true;
var b_flag2=b_flag&& A;
Console.log (B_FLAG2);

var B_flag=false;
var b_flag2=b_flag&& A;
Console.log (B_FLAG2);

-Conditional operation

? :

# # #运算符的优先级
----
| Priority | Type of Operation | Operator using |
19 Parenthesis ()
Access to array access by the new member. []
17 Function call FN
16 post increment and decrement + +--
15 logical non, forward increment decrement typeof! ++ --
14 multiplication to take the remainder *\%
13 Plus minus +-
11 greater than or equal to <>=
10 equals all equals not equal = = = = = = = = = = = = = = = !==
6 Logic and &&
5 Logic or | |
43-dollar operation? :
3 Assigned value =

1, good at using parentheses operator
2, remember the precedence of operators

Assignment is very low, the right expression is evaluated first, and then the left side is assigned
# # #程序顺序执行结构
----

The sequential structure of the program is strictly in accordance with the code in the program execution, if there is a choice structure, according to the specific conditions
Execute the appropriate code. The loop structure will also have conditional judgments, which will execute different code depending on the situation. Some
There are also interactive technologies that respond to user requirements or external circumstances.

JS Basics-2

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.