In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I would like to share with you the basic elements of javascript grammar what are the relevant knowledge points, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's learn about it.
The basic elements of javascript syntax are values, operators, expressions, keywords, and comments; each statement in javascript ends with a semicolon, and if a statement is on one line, you don't have to write a semicolon.
This article operating environment: Windows7 system, javascript1.8.5 version, DELL G3 computer
JavaScript syntax
What are the basic elements of a js statement, and what do you need to pay attention to when using js values and operators? What is grammar? in fact, grammar is a set of rules. Just like when we learn Chinese, we learn subject-predicate-object and fixed complement. JS also has its own grammar and its own set of rules.
Just as we speak, what we say is made up of columns, and JS programs are made up of columns.
We speak sentence by sentence, and the execution of the program is also the execution of sentence by sentence.
At the front end, JS programs are executed by browsers.
JS statements are made up of values, operators, expressions, keywords, and comments. This is just like every sentence we usually say is made up of one word after another.
Each statement ends with a semicolon.
If a statement is on one line, you don't have to write a semicolon. However, I suggest you get into the habit of writing semicolons, which will avoid a lot of unnecessary trouble.
JS ignores multiple spaces. To improve readability, I suggest you add spaces to your code, such as the following two lines of code:
Var name= "Liu Xiaoniu"; var name= "Liu Xiaoniu"
These two sentences are the same, but in the first sentence we put spaces on the left and right sides of the equal sign to make it look much more comfortable.
JS statements can be enclosed in curly braces {}, and the code in curly braces is called a code block. Code blocks are usually used in functions, which we will talk about later.
Let's introduce the values, operators, expressions, keywords, and comments in the JS statement.
-comment
It is necessary to add comments appropriately to the code. It not only enhances the readability of the code, but also facilitates the debugging of the code.
Comments are mainly used to add notes to the code, explaining the functions of the current code.
The contents of the comments are not executed.
Comments are divided into single-line comments and multi-line comments.
Single-line comments begin with / /, and the rest of the / / will not be executed. For example:
/ / assign the value 1var a = 1; var b = 2; / / assign the position of an one-line comment to the variable b, usually on a single line above or after the code. Multiline comments begin with / * and end with * /. This is the same as the comments in CSS. Multiline comments are also called comment blocks. Write an example. / * the following code assigns variables an and b the value of an is 1 and the value of b is 2. * / var a = 1; var b = 2; in general, a single-line comment is sufficient.
Comments are also often used when we debug our code. For example:
/ / var a = 1 * var a = 2
We can add comments without typing the code back and forth.
-the value of JS
The JS statement defines three types of values: mixed values, variable values, and constant values.
Mixed values are called literals, variable values are called variables, and constant values are called constants.
Literal quantity is also called direct quantity, that is, what you see is what you see. It can also be understood as a value. For example, the numerical value, 1 is 1, and 2 is 2, or it can also be a decimal, such as a string, which is text, surrounded by double or single quotes, such as "Liu Xiaoniu" or "Liu Xiaoniu". It can also be an array or an object.
Literals usually appear to the right of the assignment operator, which is described separately below the assignment operator. For example:
Var a = "Liu Xiaoniu"; / / an is a variable, and the string Liu Xiaoniu to the right of the equal sign is literal.
Variables and constants are used to store data values. They are containers.
Variables are declared with the keyword var, and constants are declared with const.
The difference between a variable and a constant is that a variable is a stored variable value that can be assigned multiple times; a constant stores an immutable value and cannot be assigned multiple times.
When a constant is declared, it must have an initial value, and the variable can be absent. In general, constants are capitalized.
The three values of JS are usually used in assignments.
Operator of-JS
The operator should be no stranger to us. We learned it in primary school math. For example, the arithmetic operators are: addition, subtraction, multiplication and division, and the comparison operators are greater than, equal to, and less than. The operators in JS are similar to those in mathematics, except that they are a little more than those in primary school mathematics, with a slight difference. Let's introduce them one by one.
Common operators for JS include the following:
Assignment operator
Arithmetic operator
Comparison operator
Logical operator
Type operator
Assignment operator: (=)
The assignment operator is an equal sign, which we use for comparison in primary school math. But in JS, an equal sign is used for assignment. When we introduced values above, we said that the three values of JS are usually used in assignment, which is achieved through the equal sign. For example:
Var a = 1; / / A to the left of the equals sign is a variable, 1 to the right of the equals sign is a literal quantity, and the equals sign is an assignment operator that assigns 1 to the variable a.
An equal sign is the most basic assignment operator, and there are some more advanced assignment operators that not only assign values, but also have the function of logical calculation, which we will introduce after we have introduced several other operators.
The arithmetic operator of-JS
Addition operator: (+)
The addition operator is a bit special because in JS, the plus sign (+) also indicates a join. Therefore, you need to look at the types of values on both sides of the plus sign.
1. There are numbers on both sides of the plus sign, and the result is the result of the addition of two numbers. For example:
Var a = 1 + 1; the value of / / an is 2
2. There are strings on both sides of the plus sign, and the result is two strings concatenated. For example:
Var a ='I am Liu Xiaoniu'+ 'habitat'; / / the result is: "I am Liu Xiaoniu's habitat"
3. there is a number and a string on both sides of the plus sign, and the result is a string spliced into a number and a string. For example:
Var a = 1 + 'Liu Xiaoniu'; / / a result is: "1 Liu Xiaoniu" var a = 'Liu Xiaoniu' + 10; / / a result is "Liu Xiaoniu 10"
Subtraction operator: (-)
Multiplication operator: (*)
Division operator: (/)
The functions of subtraction, multiplication and division are the same as our mathematical operators, except that the symbols are not quite the same. Multiply with an asterisk and divide with a slash.
Coefficient operator: (%) it returns the remainder. For example:
Var a = 8% 3; the value of / / an is 2
Increment operator: (+) increments a numeric value, such as:
Var a = 1. The value of / b is 2.
Increment is to add 1 to the original.
Decrement operator: (-) decrements the numerical value
Var a = 10 *
Decreasing is minus 1 on the original basis.
Tell me the difference between averse + and + + a. When assigning values, the order in which they are executed is different. For example:
The value of var a = 1 * * var b = *
A value + is assigned first and then incremented.
Var a = 10 position var b = + + a; / b is 11.
+ + an is incremented first and then assigned.
A-and-an are similar. A-first assigns and then decreases,-a decreases first and then assigns.
This is also easy to remember, that is, whoever comes before the value and operator will be executed first.
The priority of the operator is the same as in primary school mathematics, the order of calculation is from left to right, multiply and divide first, then add and subtract, if there are parentheses, calculate the parentheses first.
Comparison operator of-JS
Greater than operator: (>)
Less than operator: (>)
Greater than or equal to operator: (> =)
Less than or equal to operator: (0)? 2: 3; / / b is 2
The ternary operator, which we will mention later when we talk about conditional statements.
Logical operator of-JS
Logic and operators: (& &)
Logic or operator: (| |)
Logical non-operator: (!)
There are three logical operators, and, OR, and not. They are generally used in the judgment of conditional statements.
And and or generally connect multiple judgment conditions, which are usually connected by comparison operators.
If the result of multiple operations of logic and connection is true, its result will be true, otherwise it will be false.
A logical or connected result of multiple operations, if one is true, it is true.
For example:
Var a = 1; var b = 5; var c = (a > 0 & & b)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.