JS Syntax overview

Source: Internet
Author: User
Tags bitwise operators

# syntax Overview

# # How JavaScript is introduced
1. Write code directly inside the ' Script ' tab

"' HTML
<! DOCTYPE html>
<meta charset= "Utf-8" >
<title></title>
<script type= "Text/javascript" >
Console.log (' Hello, world ');
</script>
<body>
</body>
```
2. Introduce an external JavaScript file via the ' src ' attribute of the ' script ' tag

"' HTML
<! DOCTYPE html>
<meta charset= "Utf-8" >
<title></title>
<body>
<script type= "Text/javascript" src= "./index.js" ></script>
</body>
```

# # Data type
There are 5 primitive types in JavaScript
-Numeric type (' number ')
-String type (' string ')
-Boolean value type (' Boolean ')
-NULL
-Undefined

# # variable naming rules
Naming rules:
-The first character can be any Unicode uppercase and lowercase letters, as well as a dollar sign ($) and an underscore (_).
-The second character, followed by the character, can also be used as a number.
-Cannot use reserved word as variable name

##### *javascript Reserved Words *
' Abstract '
' Boolean ' ' Break ' ' byte '
' Case ' ' catch ' ' char ' ' class ' const ' Continue '
' Debugger ' default ' delete ' do ' double '
' Else ' enum ' ' export ' ' extends '
' False ' final ' finally ' float ' for ' function '
' Goto '
' If ' ' Implements ' ' Import ' ' in ' instanceof ' int ' interface '
' Long '
' native ' new ' null '
' Package ', ' private ' ' protected ' public '
' Return '
' Short ' static ' super ' ' Switch ' synchronized '
' This ' ' throw ' ' throws ' transient ' true ' try ' typeof '
' var ' void ' ' volatile '
' While ' with '

*example*
# # # Legal naming rules
' Arg0 '
' _tmp '
' $elem '
' π '
# # # Illegal naming rules
' 1a '
' 23 '
`***`
' A+b '
'-d '
' var '
' Return '

# # operator
ECMA-262 describes a set of operators that manipulate data values, including arithmetic operators such as plus and minus signs, bitwise operators, relational operators, and equality operators.

# # # unary operator
1. Increment and decrement operators (' + + '--')
2. Unary Plus and minus operators
###### when a unary plus operator is applied to a non-numeric value, the operator casts the value to a numeric type

# # # arithmetic operator
`+` `-` `*` `/` `%`

# # # logical operator
Different types of values are converted to Boolean values
##### object = ' true '
##### empty string = ' false '
##### non-empty string = ' true '
##### value 0 = ' false '
##### any non-0 value (including ' Infinity ') = ' true '
##### ' null ' = ' false '
##### ' NaN ' = ' false '
##### ' undefined ' = ' false '

1. Logical non ('! ')
The ###### logical non-operator has an exclamation mark ('! ') that can be used for any value in JS. Regardless of what data type this value is, the operator returns a Boolean value.
###### to manipulate return values for different types of values
###### 1. Returns ' false ' if the value is an object
###### 2. Returns ' true ' if the value is an empty string
###### 3. Returns ' false ' if the value is a non-empty string
###### 4. If the value is a value of ' 0 ', return ' true '
###### 5. If the value is any non-' 0 ' numeric value (including ' Infinity '), return ' false '
###### 6. Returns ' true ' if the value is ' null '
###### 7. Returns ' true ' if the value is ' NaN '
###### 8. If the value is ' undefined ', return ' true '
2. Logic and (' && ')
###### logic with two and number (' && ') indicates that there are two operands, you can manipulate any of the two values in JS. The operator returns a Boolean value when all two data values are Boolean values. If there is a value that is not a Boolean, then a Boolean value is not necessarily returned.
###### But when there is a data value that is not a Boolean value, its return results follow the following rules:
###### 1. Returns the second operand if the first operand is an object
###### 2. If the second operand is an object, the object is returned only if the value of the first operand evaluates to ' true '
###### 3. If two operands are objects, the second operand is returned
###### 4. Returns ' null ' if one of the operands is ' null '
###### 5. If one operand is ' nan ', then return ' Nan '
###### 6. If there is an operand of ' undefined ', then return ' undefined '
3. Logic or (' | | | `)
The ###### logic or operator has two vertical bar symbols (' | | | '), there are two operands, you can manipulate any of the two values in JS.
###### returns ' false ' if the two operands are Boolean, only if two operands are ' false ', otherwise ' true ' is returned.
###### If there is an operand that is not a Boolean value, the following rule is followed:
###### 1. Returns the first operand if the first operand is an object
###### 2. Returns the second operand if the evaluation result of the first operand is ' false '
###### 3. If two numbers are objects, the first operand is returned
###### 4. Returns ' null ' if all two operands are ' null '
###### 5. Returns ' Nan ' if all two operands are ' Nan '
###### 6. Returns ' undefined ' if the two operands are ' undefined '

4. Short-circuit operation
###### in ' with ' operator (' && ') and ' or ' operator (' | | | '), if the first operand is already able to determine the result of the final return, then the second operand is not evaluated


# # relational operator
The relational operators, which are less than (' < '), greater than (' > '), less than equals (' <= '), greater than or equal (' >= '), are used to compare two values and compare the same rules as we learned in the math class. These operators will return a Boolean value.

# # equality operator
Equality operator (' = = '), inequality operator ('! = '), equality operator (' = = = '), non-congruent operator ('!== ')
# # # equal and Unequal # = First conversion type again comparison
# # # # congruent and not congruent = only compare and not convert type

# # Assignment operator
The simple assignment operator is represented by the equals sign (' = '), and the function is to assign the value on the right to the variable on the left.

# # conditional operator (trinocular operator)
' Boolean_expression? True_value:false_value '

The precedence of the # # operator

| operator | Description |
| ---------------------------------------- | -------------------------- |
| ' + + '--'-'-' + ' ~ '! ' delete ' new ' typeof ' void ' | Unary operators, return data types, object creation, undefined values |
| `*` `/` `%`                              | Multiplication, Division, modulo |
| `+` `-` `+`                              | addition, subtraction, String connection |
| ' << ' >> ' >>> ' | Shift |
| ' < ' <= ' > ' >= ' instanceof ' | Less than, equal to, greater than, greater than or equal to, instanceof |
| `==` `!=` `===` `!==`                    | Equal, not equal, strictly equal, not strictly equal |
| ' && ' | Logic and |
| `\|\|`                                   | Logical OR |
| `?:`                                     | Conditions |
| `=`                                      | Assignment, Operation Assignment |

The following table lists the JavaScript operators by priority from highest to lowest. Operators with the same precedence are evaluated in order from left to right.

# # code Comment
The part of the source code that is ignored by the JavaScript engine is called a comment, and its purpose is to interpret the code. JavaScript provides two types of annotations: one is a single-line comment, with a '//' Start, the other is a multiline comment, placed between '/* ' and ' * * '.

"' JavaScript
Single-line Comment

/*
*
* Multi-line Comment
*
**/
```

JS Syntax overview

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.