SoFunction
Updated on 2025-03-01

JavaScript basic syntax js expression

This article will introduce javascript expressions in detail, which are divided into original expressions and complex expressions. Generally speaking, the terms that people hear more about JavaScript basic syntax are operators and statements. However, there is actually another term that is often used but rarely mentioned, which is javascript expression.

Primary expression

The original expression is the smallest unit of an expression - it no longer contains other expressions. Original expressions in javascript include this keyword, identifier reference, literal reference, array initialization, object initialization, and grouping expressions

PrimaryExpression : 
this 
Identifier 
Literal 
ArrayLiteral 
ObjectLiteral 
( Expression ) 

This keyword and identifier

this;//Return the current object

i;//Return the value of variable i

sum;//Return the value of the variable sum

Literal

Literal, translated into direct quantity, is the data value directly used in the program

Literal ::
NullLiteral
BooleanLiteral
NumericLiteral
StringLiteral 
RegularExpressionLiteral 
null;
undefined;
true;
false;
1;
'abc';
/pattern/; 

Array and object initialization

Array initialization and object initialization are actually a process of initialization described literally. These two initialization expressions are sometimes called "object direct quantity" and "array direct quantity"

[];
[1,2,3];
{};
{a:1}; 

Grouping expressions

Grouping expressions are actually brackets, used to override operator priority

Complex expression (MemberExpression)

Complex expressions are composed of original expressions and operators, including attribute access expressions, object creation expressions, and function expressions.

MemberExpression : 
MemberExpression [ Expression ] 
MemberExpression . IdentifierName 
new MemberExpression Arguments
FunctionExpression 

Attribute access expression

The attribute access expression operation can obtain the value of an object attribute or an array element. JavaScript defines two syntaxes for attribute access

MemberExpression . IdentifierName 
MemberExpression [ Expression ] 

The first way is to write an expression followed by a period and an identifier. The expression specifies the object, and the identifier specifies the name of the attribute to access.

The second way to write it is to use square brackets, which is another expression inside the square brackets (this method is suitable for objects and arrays). The second expression specifies the name of the attribute to access or the index representing the array element to access

var o = {x:1,y:{z:3}}; //Object literalvar a = [o,4,[5,6]]; // Array literals containing objects;//The x attribute of expression o;//The z attribute of the expressiono['x'];//The x attribute of object oa[1];//expressionaThe index is1Elements 

Regardless of the form of attribute access expressions used, the expressions before '.' and '[' are always evaluated first.

If the result of the calculation is null or undefined, the expression will throw a type error exception because neither of these values ​​can contain any attributes

If the calculation result is not an object, javascript converts it to an object

If the object expression is followed by a period and an identifier, the property value specified by this identifier is found and returned as the value of the entire expression

If the object expression is followed by a pair of square brackets, the value of the expression in the square brackets is calculated and converted to a string

In either case, if the named attribute does not exist, then the value of the entire attribute access expression is undefined

Object creation expression

Object creation expression creates an object and calls a function to initialize the properties of a new object

new Object();
new Point(2,3); 

If an object creates an expression without passing any arguments to the constructor, then this pair of empty brackets can be omitted

new Object;

Function expressions

Function expressions are divided into function definition expressions and function call expressions

Function definition expression defines a javascript function, and the value of the expression is this newly defined function

A typical function definition expression contains the keyword function, followed by a pair of parentheses, with a comma-separated list, which contains 0 or more identifiers (parameter names), and then followed by a curly braces.

JavaScript code segment (function body)

function square(x){
return x*x;
} 

Function definition expressions can also contain the name of the function, and functions can also be defined through function statements, rather than function expressions.

var square = function(x){return x*x;} 

A function call expression is a syntax representation that calls or executes a function or method. If this expression is an attribute access expression, then this call is called a method call

f(0);
(x,y,z);
();

The above is the relevant content of the basic JavaScript syntax js expressions introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!