JavaScript programming language is one of the most powerful and widely used programming languages in the web world. This language offers powerful design and high flexibility in programming and web pages to site programming.
One of the charms of JavaScript is expressions and operators. Expressions and operators in JavaScript provide the programmer and website developer with a powerful platform for creating program logic and performing operations.
In this content, it examines the most important JavaScript expressions and operators and examines their usage and applications.
Table of Contents
What is Expression and Operator in JavaScript?
In JavaScript, an expression is a piece of code that evaluates a value and returns the result as a value. An expression may be a combination of operators, variables, constants, functions, and boolean values.
Operators play a very important role in JavaScript. Using operators, you can perform mathematical, logical, and string operations and compare data. Consequently, expressions and operators allow programmers to increase the power and efficiency of programs.
1. Assignment operators
Assignment operators assign a specified value to the variable on their left. The simplest assignment operator is the equal operator (=). For example, the statement x=f() assigns the value f() to the variable x.
2. Comparison Operators
In JavaScript, comparison operators are used to compare two values with each other and their result is a logical value (true or false).
3. Arithmetic Operators
Arithmetic operators are used in JavaScript to perform mathematical operations. These operators allow the programmer to perform mathematical operations such as addition, subtraction, multiplication, and division of numbers.
4. Bitwise Operators
Bitwise operators are used in JavaScript to perform operations on binary numbers. These operators allow the programmer to have finer control over the bits of two numbers and perform operations such as scaling, shifting, and bit shifting.
5. Logical Operators
Boolean operators are used with Boolean values and return a Boolean value.
These operators allow the programmer to check various conditions in programs and perform logical operations such as AND, OR, and NOT.
6. BigInt operator
In JavaScript, BigInt operators are used to perform mathematical operations on large numbers. The BigInt operators allow the programmer to work with numbers that are larger than the maximum range of normal numbers in JavaScript. For example, numbers with hundreds or thousands of digits.
Using these operators, you can perform addition, subtraction, multiplication, division, exponentiation, and other mathematical operations on BigInt numbers.
7. String Operator
String operators are used in JavaScript to perform operations related to strings (texts). String operators allow the web developer to concatenate, compare, and replace strings.
8. Conditional Operator
Ternary conditional operators are used in JavaScript to determine the value of a variable based on a condition. These operators allow the programmer to assign a value to a variable based on a condition in one line of code. As a result, the code will be simpler and more readable.
The conditional operator evaluates the value placed in the condition section. If the condition is true (TRUE), the value value 1 is assigned to the variable. Otherwise (the condition is FALSE), value2 is assigned to the variable.
9. Comma Operator
The comma operator in JavaScript is used to perform multiple operations in one code sentence. This operator allows the site developer to perform multiple operations in a single line of code and retrieve their results in sequence.
The comma operator first evaluates `expression1′ and then goes to `expression2′ and evaluates it, and so on until the end of the expression. The value of the last `expression’ in this chain is considered the overall result of this expression.
10. Unary Operators
Unary operators in JavaScript are operators that have only one operand. In the following, some unary operators are introduced.
1. The delete operator
The delete operator in JavaScript is used to remove an attribute or an element from an object.
Here, `object’ indicates the object on which you want to perform the delete operation. `property’ is the name of the property you want to remove, and `index’ is the position of the element you want to remove from the object.
2. typeof operator
The typeof operator in JavaScript is used to get the data type of a value or a variable. This operator allows the site programmer to recognize the data type and perform appropriate operations based on it.
The result of the typeof operator returns a string as output that represents the data type. Some possible results include:
- “string” for strings,
- “number” for numbers,
- “boolean” for boolean values (true and false),
- “object” for objects and arrays,
- “undefined” for variables that are not initialized,
- “function” for functions,
- “Symbol” for symbols.
3. Void operator
The void operator in JavaScript is used to execute an expression and return an empty result (undefined). In other words, the void operator is used to execute an expression without expecting or returning any value to the main code.
4. This operator
This operator in JavaScript refers to the current value of the running object. Simply put, this allows the programmer to access the properties and methods of the current object it is contained within.
5. Operator new
The new operator in JavaScript is used to create a new instance of an object (or the template on which objects are created). This operator allows the programmer to create new instances of the object using a class or constructor function as the main template.
6. Superoperator
The super operator is used in JavaScript to access the properties and methods of an object’s parent (or parent class). Using super, you access the parent class members that are created as child objects in the current class.