Expressions and Assignments
Expressions and Assignments
Arithmetic Expressions
JavaScript also has operator precedence in performing arithmetic expressions. The higher precedence will be evaluated first and each operator has either left-to-right or right-to-left associativity rules. This shows the direction of the expressions evaluation.
Most binary operators have left-to-right associativity while unary and assignment operators have right-to-left associativity.
JavaScript also might have functional side effects that will affect the result of execution.
The example shows the pure function of JavaScript without any side effects. The calculation is evaluated from left to right (4+5+10), producing the output of 19. This code did not alter any of the function values.
However, if there is an external state outside of the function, the impure function of JavaScript causes a side effect of the expression. The calculation is evaluated from left to right, but the value used is in the local parameter, add() function. The output is 10, instead of 19.
The best practice in handling JavaScript is to use pure functions, minimizing the function side effects. It can maintain the code simplicity and avoid unintended behavior of values.
- Nur Insyirah Iman
Overloaded Operators
JavaScript does not support user-defined overloaded operators which the operator cannot be redefined for specific purposes. For JavaScript and Node.js, the arithmetic expressions can be overridden by using valueOf or toString method, especially to define the behaviour for arithmetic operators.
valueOf() method is used for numeric operations such as +, -, * and / into a primitive value.
toString() method is used to defines an object into a string for string operations
- Nur Insyirah Iman
Type Conversions
Type Conversions refers to the automatic or explicit process of converting data from one data type to another (JavaScript - Type Conversions, n.d.). In JavaScript, there are two categories of type conversion, which are the Implicit Conversion and Explicit Conversion.
1. Implicit Type Conversion (Coercion)
Implicit Conversion refers to the automatic process of data type conversion. In the case of JavaScript, users can use an operator with a string and another data type to perform implicit type conversion.
Converting to String
In line 2, number 24 is converted from number into string.
In line 3, boolean value false is converted from boolean into string.
In line 4, keyword null is converted into string.
Converting to Number
In line 2, the string "100" is converted from string into number.
In line 3, boolean value false is converted from boolean into number (0).
In line 4, the string "anon" is converted from string into NaN.
Converting to Boolean
By using (!!) with any variable, the value of the variable is converted to the boolean value.
In line 2, if !0 is true, then !!0 is false.
In line 3, if !1 = false, then !!1 = true.
In line 4, if !"Hello" is false, then !!"Hello" is true.
Null to Number
In line 2, the null represents the empty.
When null is used as an operand of the arithmetic operator, the null automatically converted to 0.
Undefined with Number and Boolean
By using the keyword undefined, the number or boolean value of it always gives NaN as output.
NaN is not a number.
2. Explicit Type Conversion
Explicit Conversion refers to the manual process of data type conversion. In the case of JavaScript, users can use constructors or built-in functions to perform explicit type conversion.
In line 2, number 100 is converted from number into string.
In line 3, keyword null is converted into string.
In line 3, boolean value true is converted from boolean into string.
In line 2, the string "100" is converted from string into number.
In line 3, boolean value false is converted from boolean into number (0).
In line 4, the keyword null is converted into number (0).
In line 2, number 100 is converted from number into boolean (true).
In line 3, the string "Hello" is converted from string into boolean (true).
In line 4, the keyword null is converted into falsy value (false).
- Sofia Batrisyia
Relational & Boolean Expressions
Now, we will delve into the operators in JavaScript and Node.js. JavaScript operators are symbols used to perform operations on values and variables known as operands. These operators can be classified into several categories based on their functionality.
Source: https://www.w3schools.com/jsref/jsref_operators.asp
source: https://www.w3schools.com/js/js_comparisons.asp
source: https://www.w3schools.com/jsref/jsref_operators.asp
Boolean expressions involve logical operators to combine or negate relational expressions.
source: https://www.w3schools.com/js/js_comparisons.asp
OR operator: returns true if any of the variables are true; otherwise, it would produce a false value.
AND operator: returns false if any of the variables are false; otherwise, it would return a true value.
NOT operator: it is used to invert the value of the operand.
There are no XOR operator in JavaScript.
(W3Schools.com, n.d.) (Tuama, 2023)
Relational Expressions and Boolean Expressions from Sample Applications
1. Equality (===)
This expression is used to check whether the user's input matches either an empty string (" ") or "no".
2. Logical OR ( || )
The condition evaluates to true if either of the two conditions is true.
3. Logical NOT ( ! )
This expression negates the result of response.ok, turning true into false and vice versa.
- Kueh Pang Teng
Short-Circuit Evaluation
Short-circuit evaluation indicates the process of evaluating expressions from left to right, which stops as soon as it reaches the result. The remaining conditions are disregarded if the result of an expression is obvious before assessing all of the conditions.
AND(&&) operator
In this section of codes, it uses the AND (&&) operator. If the first condition, 'message' is false or undefined, It will not evaluate the second condition, "message.tolowerCase().includes(keyword)" because the entire condition can never be true.
OR(||) operator
In this snippet of code, it uses OR(||) operator to evaluate to true if either condition 1, "message" is false, or condition 2," message.toLowerCase().includes(keyword) is false. If one of the conditions is true, the function will exit early with return. Otherwise, if both conditions are false, the code proceeds to execute the following block of codes.
While short-circuit assignments do have a bunch of benefits, there are undeniably possible functional side effects of not using them properly.
Functions or expressions on the right side might not be executed, regardless of whether the value is true or false.
2. Harder for developers to search for bugs. For example, a bug may happen when a function modifies a variable outside its scope, that cannot be explicitly seen.
3. It may accidentally go to outer scopes and modify the values, thus affecting the output.
- Anis Syifaa'
Assignment Statements
The assignment statement (=) is used to assign values to variables. The operator has its specific value, which is 'assigned'. The value allows multiple assignments to be tied together. Therefore, a value can be assigned to multiple variables in a single program.
source: https://www.w3schools.com/js/js_assignment.asp
Assignment Operator
The assignment operator assigns values to a variable.
'=' : the simplest form of assignment operator. It is used to assign a value to a variable.
'+=' : the addition assignment operator can be used to add a value to a variable
'-=' : the subtraction assignment operator subtracts a value from a variable
'*=' : the multiplication assignment operator multiplies a variable with the assigned value
'**=' : the exponentiation assignment increases a variable by raising it to the power of a given number
'/=' : the division assignment operator divides the value of a variable with a given number
'%=' : the remainder assignment operator assigns a remainder to a variable
source: https://www.w3schools.com/js/js_assignment.asp
Shift Assignment Operator
The shift assignment applies the bitwise shift operation to a variable and then assigns the result back to the variables.
'<<=' : the left shift assignment operator shifts a variable to the left
'>>=" : the signed right shift assignment operator shifts a variable to the right
'>>>=' : the unsigned right shift assignment operator shifts a variable to the right
source: https://www.w3schools.com/js/js_assignment.asp
Bitwise Assignment Operator
Bitwise assignment operators perform operations at the binary level (bit). These assignment operators are useful for tasks that require the manipulation of individual bits such as encryption. It will operate on the left and the right operands, then assign the result back to the left operand.
'&=' : the bitwise AND assignment operator will return value 1 if both bits are 1. Otherwise, it will return 0.
'|=' : the bitwise OR assignment operator returns the value 1 if either or both bits are 1. If both bits are 0, it will return the value as 0.
'^=' : the bitwise XOR assignment operator returns the value as 1 when exactly one of the bits is 1. Otherwise, it will return 0.
source: https://www.w3schools.com/js/js_assignment.asp
Logical Assignment Operator
The logical assignment operators are used to combine two or more conditions and return a single true/false result.
'&&=' : the logical AND assignment operator will assign the second value if the first value is true.
'||=' : the logical OR assignment operator will assign the second value if the first value is false.
'??=' : the nullish coalescing assignment operator will assign the second value if the first value is undefined or null.
(W3Schools.com, n.d.-c)
- Anis Syifaa'
Mixed-Mode Assignment
Assignment statements in mixed-mode, also known as mixed-mode expressions are allowed in some languages. In order to do so, the specific languages must define conventions for implicit operand type conversions because computers do not have binary operations that can take operands of different types (Comrevo, 2021).
In the case of JavaScript:
a is an integer variable that holds number 4.
b is a float variable that hold number 3.14.
When a is added with b, the result of the operation is printed as c.
- Sofia Batrisyia
REFERENCES
Arithmetic Expressions
GeeksforGeeks. (2023, December 28). Operator precedence in JavaScript. GeeksforGeeks. https://www.geeksforgeeks.org/operator-precedence-in-javascript/
Perera, R. S. (2024, December 10). Pure and Impure Functions in JavaScript: A complete guide. Syncfusion. https://www.syncfusion.com/blogs/post/pure-impure-functions-javascript
Overloaded Operators
Tharwat, N. (2021, April 2). Deep dive beyond operator overloading in JavaScript. DEV Community. https://dev.to/kl13nt/deep-dive-beyond-operator-overloading-in-javascript-2a48
Type Conversions
Parewa Labs Pvt. Ltd. (2025). JavaScript Type Conversions (with Examples). (n.d.). https://www.programiz.com/javascript/type-conversion
tutorialpoints (2025). JavaScript - type conversions. https://www.tutorialspoint.com/javascript/javascript_type_conversions.htm
Relational and Boolean Expressions
Tuama, D. Ó. (2023, April 24). JavaScript Operators - Code Institute IE. Code Institute IE. https://codeinstitute.net/global/blog/javascript-operators/
W3Schools.com. (n.d.). https://www.w3schools.com/jsref/jsref_operators.asp
W3Schools.com. (n.d.-b). https://www.w3schools.com/js/js_comparisons.asp
Short-Circuit Evaluation
GeeksforGeeks. (2024, September 26). Javascript short circuiting operators. GeeksforGeeks. https://www.geeksforgeeks.org/javascript-short-circuiting/
Assignment Statements
W3Schools.com. (n.d.-c). https://www.w3schools.com/js/js_assignment.asp
Mixed-Mode Assignment
Comrevo. (2021, March 27). Mixed Mode Assignment | PPL | SebEsta | Expressions and Assignment Statements [Video]. YouTube. https://www.youtube.com/watch?v=mAmBML45600