search
HomeWeb Front-endJS TutorialWhat is an expression in javascript

An expression statement is actually an expression, which is composed of operators connecting variables or direct quantities. Generally speaking, the expression statement is either a function call, an assignment, or an increment or decrement, otherwise the result of the expression calculation has no meaning.

What is an expression in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

An expression statement is actually an expression, which is composed of operators connecting variables or direct quantities.

Generally speaking, expression statements are either function calls, assignments, or self-increment or self-decrement, otherwise the result of the expression calculation has no meaning.

There is no such restriction in JavaScript syntax. Any legal expression can be used as an expression statement.

a + b;

This line of code calculates the value of the addition of a and b, but it will not be displayed or produce any execution effect (unless a and b are getters), but it does not prevent it from complying with the grammar. be executed.

PrimaryExpression Primary expression

The atomic item of expression: Primary Expression. It is the smallest unit of expression, and the grammatical structure it involves also has the highest priority.

Primary Expression contains various "direct quantities". Direct quantities are values ​​of a specific type that are written directly using a certain syntax. Direct quantities are the syntax for writing them out in code.

JavaScript can define objects in the form of direct quantities. JavaScript provides grammatical support for special object types such as functions, classes, arrays, and regular expressions.

({});
(function(){});
(class{ });
[];
/abc/g;

At the grammatical level, expression statements starting with function, { and class have grammatical conflicts with declaration statements. If you want to use such expressions, you must add parentheses to avoid grammatical conflicts.

Primary Expression can also be this or a variable. In syntax, the variable is called an "identifier reference".

this;
myVarFun;

Any expression plus parentheses is considered a Primary Expression. This mechanism makes parentheses a means of changing the priority of operations.

(a + b);

MemberExpression Member Expression

Member Expression is usually used to access object members. It has several forms:

a.b;
a["b"];
new.target;
super.b;

new.target is a newly added syntax, used to determine whether the function is called by new, super is the syntax used to access the properties of the parent class in the constructor.

Member Expression was originally designed for attribute access, but due to syntactic structure requirements, the following two types are regarded as Member Expressions in the JavaScript standard:

Template with function, this one with function The named template means that each part of the template is calculated and passed to a function.

f`a${b}c`;

The new operation with a parameter list and the new operation without a parameter list have a lower priority and do not belong to Member Expression.

new Cls();

They belong to the same priority as attribute operations, but have no semantic relationship.

NewExpression NEW expression

Member Expression plus new is New Expression (New Expression can also be formed without adding new, which is an independent high-priority expression by default in JavaScript can form low-priority expressions).

New Expression specifically refers to an expression without a parameter list. The following code:

new new Cls(1);

Intuitively, it may have two meanings:

new (new Cls(1));
new (new Cls)(1);

In fact, it is equivalent to the first one. Use the code to verify:

class Cls{
  constructor(n){
     console.log("cls", n);
        return class {
           constructor(n) {
              console.log("returned", n);
            }
        }
    }
}

new (new Cls(1));

Running results: This shows that 1 is passed in as a parameter when calling Cls.

What is an expression in javascript

CallExpression Function call expression

Member Expression can also constitute Call Expression. Its basic form is Member Expression followed by a parameter list in parentheses, or the super keyword can be used instead of Member Expression.

a.b(c);
super();

This seems simple, but there are some variations to it. For example:

a.b(c)(d)(e);
a.b(c)[3];
a.b(c).d;
a.b(c)`xyz`;

The forms of these variations are almost one-to-one correspondence with Member Expression. In fact, it can be understood that if a certain substructure in Member Expression has a function call, then the entire expression becomes a Call Expression. The Call Expression loses the feature of higher priority than the New Expression, which is a major difference.

LeftHandSideExpression lvalue expression

New Expression and Call Expression are collectively called LeftHandSideExpression, lvalue expression.

An lvalue expression is an expression that can be placed on the left side of the equal sign. The JavaScript syntax is:

a() = b;

Such usage is actually grammatical, but the values ​​returned by native JavaScript functions cannot be assigned. Therefore, most of the time, the assignments we see will be other forms of Call Expression, such as:

a().c = b;

According to the design of the JavaScript runtime, it is not ruled out that some hosts will provide functions that return reference types. At this time, The assignment is valid.

左值表达式最经典的用法是用于构成赋值表达式,但是其实如果翻一翻 JavaScript 标准,就会发现它出现在各种场合,凡是需要“可以被修改的变量”的位置,都能见到它的身影。

AssignmentExpression 赋值表达式

AssignmentExpression 赋值表达式也有多种形态,最基本的当然是使用等号赋值:

a = b

等号是可以嵌套的:

a = b = c = d

连续赋值,是右结合的,它等价于下面这种:

a = (b = (c = d))

先把 d 的结果赋值给 c,再把整个表达式的结果赋值给 b,再赋值给 a。

赋值表达式的使用,还可以结合一些运算符,例如:

a += b;

相当于:

a = a + b;

能有这样用的运算符有下面这几种:

*=、/=、%=、+=、-=、<<=、>>=、>>>=、&=、^=、|=、**=

赋值表达式的等号左边和右边能用的表达式类型不一样。

Expression 表达式

赋值表达式可以构成 Expression 表达式的一部分。在 JavaScript 中,表达式就是用逗号运算符连接的赋值表达式。

在 JavaScript 中,比赋值运算优先级更低的就是逗号运算符了。可以把逗号可以理解为一种小型的分号。

a = b, b = 1, null;

逗号分隔的表达式会顺次执行,就像不同的表达式语句一样。“整个表达式的结果”就是“最后一个逗号后的表达式结果”。比如之前的例子,整个“a = b, b = 1, null;”表达式的结果就是“,”后面的null。

在很多场合,都不允许使用带逗号的表达式,比如我export 后只能跟赋值表达式,意思就是表达式中不能含有逗号。

【推荐学习:javascript高级教程

The above is the detailed content of What is an expression in javascript. 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
Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.