Home>Article>Web Front-end> What are the basic elements of javascript syntax

What are the basic elements of javascript syntax

藏色散人
藏色散人 Original
2022-01-19 14:33:28 1590browse

The basic elements of JavaScript syntax are values, operators, expressions, keywords and comments; each JavaScript statement ends with a semicolon. If a statement occupies one line, there is no need to write a semicolon.

What are the basic elements of javascript syntax

The operating environment of this article: Windows7 system, javascript version 1.8.5, DELL G3 computer

JavaScript syntax

What basic elements does a js statement consist of? What should we 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 adverbial complement. JS also has syntax and its own set of rules.

Just like when we speak, the content of our speech is composed of a series of statements, and the JS program is also composed of a series of statements.

We speak sentence by sentence, and the execution of the program is also executed sentence by sentence.

On the front end, the JS program is executed by the browser.

JS statements are composed of values, operators, expressions, keywords and comments. This is just like every sentence we usually say is composed of words one by one.

Each statement ends with a semicolon.

If a statement occupies one line, there is no need to write a semicolon. However, I suggest that you develop the habit of writing semicolons, which will avoid a lot of unnecessary trouble.

JS will ignore multiple spaces. In order to enhance readability, I suggest you add spaces in the code, such as the following two lines of code:

var name = "刘小妞";var name="刘小妞";

These two sentences are the same, but, In the first sentence, we added spaces on the left and right sides of the equal sign, which makes it look much more comfortable.

JS statements can be enclosed in curly braces {}, and the code in the 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 JS statements.

- Comments

It is still necessary to add appropriate comments to the code. It not only enhances the readability of the code, but also facilitates code debugging.

Comments are mainly used to add explanations to the code and explain the functions of the current code.

The content of the comments will not be executed.

Comments are divided into single-line comments and multi-line comments.

Single-line comments start with //, and the content after // will not be executed. For example:

//给变量a赋值1 var a = 1 ; var b = 2 ; //给变量b赋值2 单行注释的位置,通常在代码上面单独一行或者代码后面。 多行注释以 /* 开头,以 */ 结尾。这个和CSS的注释一样。 多行注释也叫注释块。写个例子。 /* 下面代码是给变量a和b赋值 a的值是1,b的值是2。 */ var a = 1 ; var b = 2 ; 一般情况下,单行注释就足够了。

When we debug code, we often use comments. For example:

//var a = 1; var a = 2;

We can add comments instead of typing code back and forth.

- JS values

JS statements define 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, whatever you see is what you see. It can also be understood as a value. For example, 1 is 1, 2 is 2, or it can be a decimal, such as a string. A string is text, surrounded by double quotes or single quotes, such as: "Liu Xiaoniu", 'Liu Xiaoniu'. It can also be an array or an object.

Literals usually appear on the right side of the assignment operator, which will be introduced separately below. For example:

var a = "刘小妞"; //a是变量,等号右边的字符串刘小妞是字面量。

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 variables and constants is that variables store variable values and can be assigned multiple times; constants store immutable values and cannot be assigned multiple times.

Constants must have an initial value when declared, but variables do not need to. Generally, constants are written in uppercase letters.

The three types of values of JS are usually used in assignment.

- JS operators

operators should be familiar to us, we have learned them in primary school mathematics. 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 there are more operators than in primary school mathematics, and there is a slight difference. Let’s introduce them one by one.

Commonly used operators in JS include the following:

Assignment operator
Arithmetic operator
Comparison operator
Logical operator
Type operator

Assignment operator: (=)

The assignment operator is an equal sign. In primary school mathematics, the equal sign is used for comparison. But in JS, an equal sign is used for assignment. When we introduced values above, we said that the three types of values in JS are usually used in assignment, and assignment is achieved through the equal sign. For example:

var a = 1; //等号左边a是变量,等号右边1是字面量,等号是赋值运算符,通过赋值运算符把1赋值给了变量a。

An equal sign is the most basic assignment operator, and there are some more advanced assignment operators. They are not only assignments, but also have logical calculation functions. We have introduced this before. After several operators, we will introduce them again.

-JS的 算数运算符

加法运算符:(+)

加法运算符有点特殊,因为在JS里,加号(+)也表示连接。所以,需要看加号两边值的类型。

1、加号两边都是数字,结果是两个数字相加的结果。比如:

var a = 1 + 1 ; //a的值是2

2、加号两边都是字符串,结果是两个字符串拼接。比如:

var a = '我是刘小妞' + '栖息地' ; //a的结果是:“我是刘小妞栖息地”

3、加号两边一个数字一个字符串,结果是数字和字符串拼接成的字符串。比如:

var a = 1 + '刘小妞' ; //a的结果是:“1刘小妞”var a = '刘小妞' + 10 ; //a的结果是:“刘小妞10”

减法运算符:(-)
乘法运算符:(*)
除法运算符:(/)

减、乘、除,这三个和我们数学运算符的作用是一样的,只不过,符号不太一样。乘法用星号,除法用斜杠。

系数运算符:(%)它是返回余数。比如:

var a = 8 % 3; //a的值是2

递增运算符:(++)对数值进行递增,比如:

var a = 1;a++;var b = a; //b的值是2

递增就是在原来的基础上加1。

递减运算符:(–)对数值进行递减

var a = 10;a--;var b = a; //b的值是9

递减就是在原来的基础上减1。

说一下a++和++a的区别吧。在赋值的时候,它们两个的执行顺序是不一样的。比如:

var a = 1;var b = a++; //b的值1。

a++是先赋值,再递增。

var a = 10;var b = ++a; //b的值是11。

++a是先递增,再赋值。

a–和--a也是类似的。a–先赋值再递减,–a先递减再赋值。

这个也好记,就是值和运算符谁在前面,就先执行谁。

运算符的优先级和小学数学里的一样,计算顺序是从左到右,先乘除后加减,有括号的,先算括号里的。

-JS的比较运算符

大于运算符:(>)
小于运算符:(>)
大于或等于运算符:(>=)
小于或等于运算符:(
等于运算符:(==)
等值等型运算符:(===)
不相等运算符:(!=)
不等值或不等型运算符:(!==)
三元运算符:(?)

大于、小于、大于等于、小于等于这四种和数学里的一样,我就不解释了。我重点讲下面的5种运算符。

等于运算符:(==),它的作用和数学里的一个等号是一样的,因为JS里一个等号被用做赋值了,所以,等于运算符用了两个等号。

等值等型运算符:(===),等值等型运算符是三个等号,它比两个等号严格一些,不仅两个数值要相等,两个值的类型也需要相等。

JS里有很多数据类型,有字符串,有数值等等,数据类型以后如果写的话再单独写,今天简单提一下。举个例子:

var a = '123' ;var b = 123 ;

a和b,a是字符串,b是数值。

a和b用两个等号比较,是相等的;用三个等号比较是不相等的。因为它们两个的数据类型不一样。

不相等运算符:(!=)
不等值或不等型运算符:(!==)

这两个和上面的等于运算符:(==)等值等型运算符:(===)类似。只不过,它俩比较的是不相等。

在讲三元运算符之前,我先介绍一个数据类型:布尔值

它有两个值:true和false。true是真,false是假。在写条件判断的时候,我们经常会用到这两个值。true就是条件成立,false就是条件不成立。

三元运算符:(?),也叫条件运算符,它这个稍微复杂一点。它一般是用在条件赋值里。格式如下:

var a = (condition) ? value1 :value2;

括号里是条件,如果condition的值是true,也就是条件成立,a的值就是value1,否则,a的值是value2。

写个小例子:

var a = 1 ;var b = (a>0) ? 2 : 3 ; //b的值是2

三元运算符,我们在后面讲条件语句的时候,还会提到。

- JS的逻辑运算符

逻辑与运算符:(&&)
逻辑或运算符:(||)
逻辑非运算符:(!)

逻辑运算符总共三个,。它们一般是用在条件语句里的判断。

一般是连接多个判断条件,判断条件通常是用比较运算符连接。

逻辑与连接的多个运算结果都为真,它的结果才会是真,否则为假。
逻辑或连接的多个运算结果,有一个为真,它就为真。

举个例子:

var a = 1 ;var b = 5 ;var c = (a>0 && b

逻辑非是取反。如果当前是真(true),取反就是假,当前是假,取反就是真。

写个例子:

var a = 1 ;var b = 5 ;var c = (a==b); //c的值是falsevar d = !c ; //d的值是true

逻辑运算符从字面意思也能理解。
就是并且的意思;就是或者的意思;就是相反的意思。

说一下前面说的高级一点的赋值运算符吧。其实,高级一点的运算符就是把上面说的运算符综合运用了一下。列几个常用的吧。

赋值运算符:(+=),比如:a += b 等价于 a = a + b
赋值运算符:(-=),比如:a -= b 等价于 a = a - b
赋值运算符:(*=),比如:a *= b 等价于 a = a * b
赋值运算符:(/=),比如:a /= b 等价于 a = a / b
赋值运算符:(%=),比如:a %= b 等价于 a = a % b

- JS的表达式

表达式是值和运算符的组合,计算结果是值。

我们前面介绍值和运算符的时候,其实一直在写表达式。比如:

1 + 1a + 1"刘小妞" + "栖息地"

- JS的关键词

关键词是JS自己保留的单词,这些单词都是有特定功能的,我们在定义变量的时候,不能用关键词。下表是常用的关键词。

What are the basic elements of javascript syntax

推荐学习:《javascript基础教程

The above is the detailed content of What are the basic elements of javascript syntax. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn