Lesson 2 operator Operators
(This article is long and smelly, but it is extremely easy to understand. It is only suitable for beginners of JavaScript, especially those with no foundation)
2.0 ASCII and Unicode encoding
The computer can only process numbers. When you need to display letters or other symbols, the computer converts them into corresponding numbers. The computer memory stores information in bytes. Each byte can store numbers between 0 and 255. When storing letters or other symbols, one or more bytes may be required.
Two more standard codes are used in the browser:ASCII and Unicode. ASCII defines 0 to 127 to indicate 128 characters, including 33 control characters and 95 display characters. The display characters include 52 English letters (uppercase and lowercase letters) and 43 punctuation marks (including spaces ). ASCII does not define what 128-255 represents. They have different meanings depending on the extended encoding. Currently, ISO-8859-1 (Latin-1 or "Western European language") is used to define 256 values based on ASCII. Therefore, one byte can represent 256 different characters.
There are many languages in the world. The 256 characters are far from enough to express these characters. To solve this problem, Unicode encoding was born. It uses up to four bytes to represent hundreds of characters. Its first 128 characters are the same as ASCII and are compatible with it.
The most commonly used Unicode encoding isUTF-8It uses at least one byte to represent most characters, and two or four bytes to represent other characters at the same time.UTF-16Use at least 2 bytes,UTF-32Always use 4 bytes.
Many characters cannot be entered directly on the keyboard. Therefore, JavaScript provides a method to use escape characters. It uses a slash, a letter u, and four hexadecimal numbers to represent the 16-bit character encoding. For example, \ u03c0 indicates the circumference rate of π.
So far, we have seen how to create variables. You can obtain more information by knowing how to use and change the values stored in the variable. We use operators to do this.
Leave the Operator name alone. We are actually familiar with many operators.
Arithmetic Operators
Starting from the operators that can process numbers and variables that store numbers.
1 left + right
2 payment-change
3 width * height
4 pie/people
5 selection % choice
6 counter ++
7 + + counter
8 counter-
9-counter
These nine examples show how to use operators to obtain new values.
The first four are obvious, that is, addition, subtraction, multiplication, division. Obviously, the calculation results do not change the value of the variable, but return a new value after calculation. We need to assign the calculated result to a variable to call it.
The fifth operator % is a modulo operation, that is, the remainder after division of two numbers. For example, the result of 11% 3 is 2.
The last four are different from the first five because they change the value of the variable. Counter ++ and ++ counter both increase the variable counter by 1; counter-and-counter both reduce counter by 1. ++ Or-there is no difference between placing the variable before or after it is used independently. It is different from other operators when used together.
During computing, we usually combine several operators. The calculation result depends on their priority and combination. Now let's take a look at the priority and associativity of operators in JavaScript. Fortunately, many rules are the same as what we learned at school.
1 "++ variables" and "-variables" must first add or subtract 1 of the variables before calculation.
2 "variable ++" and "variable-" must first involve the variable in calculation, and then add or subtract 1 from the variable.
3 addition, subtraction, multiplication, division, and modulo operation, from left to right.
You can use parentheses to change the priority order. Operations in parentheses are always performed first.
- Three pages in total:
- Previous Page
- 1
- 2
- 3
- Next Page