Skip to main content

Mathematical Operators in JavaScript

JavaScript Fundamentals: Basic Operators, Math

What is an operand in JavaScript?

View Answer:
Interview Response: In programming, we apply operands to operators. They can be left- or right-hand operands and a single argument or value depending on the case.

Technical Response: Operands are expressions or values on which an operator operates or works (often constants or variables, but sub-expressions are also permitted).

Explain the difference between a Binary and a Unary operand?

View Answer:
Interview Response: A binary operand has two arguments on the left and right sides of the operator, and a Unary operand has a single operand.

Technical Response: There are two types of mathematical operators: unary and binary. Unary operators act with a single operand, and Binary operators perform actions with two operands. Unary operators are arithmetic operators that act on a single operand.

Example of Unary Operand:

let x = 1;

x = -x;

alert(x); // -1, unary negation was applied

Example of Binary Operands:

let x = 1,
y = 3;

alert(y - x); // 2, binary minus subtracts values

What basic mathematical operations are allowed in JavaScript?

View Answer:
Interview Response: The basic mathematical operations include addition, subtraction, multiplication, division, remainder or modulo, and exponentiation.

Technical Response: The basic mathematical operations allowed in JavaScript include Addition +, Subtraction -, Multiplication *, Division /, Remainder or Modulo %, and Exponentiation **.

In JavaScript, what is the remainder/modulo operator?

View Answer:
Interview Response: The remainder operator is used to find the remainder of two arguments (operands/values).

Code Example:

alert(5 % 2); // 1, a remainder of 5 divided by 2
alert(8 % 3); // 2, a remainder of 8 divided by 3

How does the exponentiation operator function in JavaScript?

View Answer:
Interview Response: The exponentiation operator multiplies a number by itself a specified number of times.

Technical Response: The exponentiation (**) operator multiplies a number by itself a specified number of times. It is like the caret (^) operator in python.

Code Example:

console.log(2 ** 2); // 4  (2 multiplied by itself 2 times)
console.log(2 ** 3); // 8 (2 * 2 * 2, 3 times)
console.log(2 ** 4); // 16 (2 * 2 * 2 * 2, 4 times)

Can exponentiation be defined for non-integer numbers?

View Answer:
Interview Response: Mathematically, the exponentiation operator gets defined for non-integer numbers. We should note that the exponent used on fractions requires parentheses to ensure proper programmatic conversion.

Code Example:

alert(4 ** (1 / 2)); // 2 (power of 1/2 is the same as a square root)
alert(8 ** (1 / 3)); // 2 (power of 1/3 is the same as a cubic root)

Is it possible to concatenate strings in JavaScript?

View Answer:
Interview Response: Yes, We can implement string concatenation using the binary plus operator or the concat method.

Technical Response: Yes, concatenation using the binary (+) operator. We can also use the built-in “concat” method to achieve the same result.

Code Example:

let s = 'my' + 'String';

alert(s); // myString
// Using the Built-in concat() method

let a = 'concat';

alert(a.concat('String')); // concatString
// String Conversion

alert('1' + 2); // "12"
alert(2 + '1'); // "21"
// Order of Operations still applies

alert(2 + 2 + '1'); // returns 2 + 2 = 4 and 4 + '1' = 41

// returns "41" and not "221"

How does unary plus work on single values?

View Answer:
Interview Response: Unary plus has no effect on numbers, but it does convert non-number strings into numbers.

Technical Response: Unary (+) plus has no conversion effect on numbers, but it converts non-numbers like strings to numbers. JavaScript also has a built-in Number(value) method that you can use to achieve the same output.

Code Example:

// No effect on numbers
let x = 1;

alert(+x); // 1
let y = -2;

alert(+y); // -2

// Converts non-numbers
alert(+true); // 1
alert(+''); // 0
alert(+'7'); // converts string “7” to number 7

If you have two strings and wish to add their values together. What technique would you use to convert both strings to integers to prevent concatenating the values into a single string?

View Answer:
Interview Response: We can use a unary plus or the number object to convert the strings to numbers and then attempt to sum the two values.

Technical Response: Since both numbers are strings (“4” + “4” = “44”). You can use an implicit or explicit approach to solve the problem. The implicit approach requires the use of unary plus applied to the left and right operand (+“4” + +“4” = 8). The second approach is the explicit use of the built-in Number Object ((Number( “4”) + Number( “4”) = 8).

Code Example:

let apples = '2';

let oranges = '3';

// both values converted to numbers before the binary plus
alert(+apples + +oranges); // 5

// the longer variant
alert(Number(apples) + Number(oranges)); // 5

What is the most prominent characteristic of all operators in JavaScript?

View Answer:
Interview Response: All JavaScript operators return a value, including the assignment (=) operator.

In what direction do chained assignments evaluate?

View Answer:
Interview Response: Chained assignments evaluate from right to left.

Code Example:

let a, b, c;

a = b = c = 2 + 2; // <- Chained Assignment right to left

alert(a); // 4
alert(b); // 4
alert(c); // 4

What is the major issue with chained assignments?

View Answer:
Interview Response: If chained assignments get implemented incorrectly, it can lead to memory leaks.

What is the difference between increment and decrement?

View Answer:
Interview Response: Increment increases a variable by one, and decrement decreases a variable by one.

Code Example:

let counter = 2;

counter++; // works the same as counter = counter + 1, but is shorter
alert(counter); // 3

////////////////////////////////

let counter = 2;

counter--; // works the same as counter = counter - 1, but is shorter
alert(counter); // 1

Can increment/decrement operators be applied to numbers?

View Answer:
Interview Response: No, We should only use increment/decrement with variables. Trying to use it on a value like 5++ gives an error.

Are operators ++ and -- placed before or after a variable?

View Answer:
Interview Response: The operators get placed before or after a variable. Before, the variable is a prefix position, and after the variable is the postfix position.

Technical Response: The operators ++ and -- get placed before or after a variable. When the operator goes after the variable, it is in “postfix position”: counter++. The “prefix position” is when the operator goes before the variable: ++counter.

Code Example:

// Postfix Position Counter
let counter = 1;
let a = ++counter; // (*)

alert(a); // 2

// Prefix Position Counter
let counter = 1;
let a = counter++; // (*) changed ++counter to counter++

alert(a); // 1

Is there any difference between the postfix and prefix increment/decrement positions?

View Answer:
Interview Response: Prefix increments the counter and returns the new value. Postfix increments counter but return the old value before being incremented.

Technical Response: Yes, the prefix form ++counter increments counter and returns the new value. The postfix form counter++ also increments the counter but returns the old value before being incremented.

Code Example:

// Prefix Position:
let counterOne = 5;
let a = ++counterOne; // (*)

alert(a); // alerts 6

// Postfix Position:
let counterTwo = 5;
let b = counterTwo++; // (*) changed ++counter to counter++

alert(b); // alerts 5

Can the ++/-- operators be used within expressions?

View Answer:
Interview Response: Technically, yes, but it is not advisable because it reduces the readability of our code.

Technical Response: Though technically okay, such notation should be avoided and usually makes code less readable. We should use a typical style of one-line action in our code.

Proper Implementation:

// We advise a style of “one line – one action”:
let counter = 1;
alert(2 * counter); // 2
counter++;

How do Bitwise operators treat arguments as 16-Bit, 24-Bit, or 32-Bit integer numbers?

View Answer:
Interview Response: Bitwise operators treat arguments as 32-bit integer numbers and work on the level of their binary representation.

Code Example:

console.log(1100 & 1011); // 1011
console.log(1100 | 1011); // 1111

What is the comma operator used for in JavaScript?

View Answer:
Interview Response: The comma operator allows us to evaluate several expressions, dividing them with a comma. Each of them gets evaluated, but only the result of the last one gets returned.

Code Example:

let a = (1 + 2, 3 + 4);

alert(a); // 7 (the result of 3 + 4)

Why do we need the comma operator that throws away everything except the last expression?

View Answer:
Interview Response: Sometimes, people use it in more complex constructs to put several actions in one line, and it is not a standard or recommended approach.

Code Example: Three expressions in one line…

// three operations in one line: Commonly used in frameworks
for (a = 1, b = 3, c = a * b; a < 10; a++) {
...
}


Does the comma operator have high or low precedence?

View Answer:
Interview Response: The comma operator has exceptionally low precedence, lower than the assignment operator.