Math Operators - JavaScript Interview Questions
JavaScript Fundamentals: Math Operators
What is an operand in JavaScript?
View Answer:
Interview Response: An operand is a value or variable used in an expression with an operator to produce a result. It can be a constant, a variable, an object, or any expression that evaluates to a value.
Can you explain the difference between a Binary, and 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;
console.log(x); // -1, unary negation was applied
Example of Binary Operands:
let x = 1,
y = 3;
console.log(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: JavaScript supports basic arithmetic operations like addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). It also supports exponential (**) and increment (++) and decrement (--) operations. These can be used to perform mathematical operations on numbers and expressions.
In JavaScript, what is the remainder/modulo operator?
View Answer:
Interview Response: TThe remainder operator, also known as the modulus operator, is used in JavaScript to find the remainder of a division operation between two numbers. In JavaScript, the modulo operator is represented by the percent sign.
Code Example:
console.log(5 % 2); // 1, a remainder of 5 divided by 2
console.log(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: Yes, exponentiation can be defined for non-integer numbers using the Math.pow() function or the exponentiation operator. Both work with non-integer and negative exponents. We should note that exponentiation used on fractions requires parentheses to ensure proper programmatic conversion.
Code Example:
console.log(4 ** (1 / 2)); // 2 (power of 1/2 is the same as a square root)
console.log(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';
console.log(s); // myString
// Using the Built-in concat() method
let a = 'concat';
console.log(a.concat('String')); // concatString
// String Conversion
console.log('1' + 2); // "12"
console.log(2 + '1'); // "21"
// Order of Operations still applies
console.log(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: In JavaScript, the unary plus operator is used to convert a value to a number. When applied to a single value, the unary plus operator performs type coercion, converting the value to a number if it's not already a number.
Code Example:
// No effect on numbers
let x = 1;
console.log(+x); // 1
let y = -2;
console.log(+y); // -2
// Converts non-numbers
console.log(+true); // 1
console.log(+''); // 0
console.log(+'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
console.log(+apples + +oranges); // 5
// the longer variant
console.log(Number(apples) + Number(oranges)); // 5
What is the most prominent characteristics of all operators in JavaScript?
View Answer:
Interview Response: All JavaScript operators return a value, including the assignment (=) operator and their ability to perform various types of operations on values or expressions.
In what direction do chained assignments evaluate?
View Answer:
Interview Response: In JavaScript, chained assignments evaluate from right to left. In simple terms, the value on the right gets assigned to the variables on the left.
Code Example:
let a, b, c;
a = b = c = 2 + 2; // <- Chained Assignment right to left
console.log(a); // 4
console.log(b); // 4
console.log(c); // 4
What is the major issue with chained assignments?
View Answer:
Interview Response: Chained assignments in JavaScript can lead to unintended side effects due to the right-to-left evaluation, making the code harder to read, understand, debug, and maintain.
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.
Technical Response: Increment increases a value by one, while decrement decreases it by one. In JavaScript, the increment operator uses a “++” sign,while the decrement operator uses a minus minus “--” sign.
Code Example:
let counter = 2;
counter++; // works the same as counter = counter + 1, but is shorter
console.log(counter); // 3
////////////////////////////////
let counter = 2;
counter--; // works the same as counter = counter - 1, but is shorter
console.log(counter); // 1
Can increment/decrement operators be attached to numbers?
View Answer:
Interview Response: No, increment/decrement operators (++, --) cannot be directly attached to numbers. They are unary operators that can only be used with variables or expressions containing variables.
Code Example:
let num = 5;
console.log(num++)
// example with direct attachment to a number
console.log(++5) // results in a SyntaxError
Are decrement and increment operators placed before or after a variable?
View Answer:
Interview Response: The operators can be placed before or after a variable. Before the variable is defined as the prefix position, and after the variable is the postfix position.
Code Example:
// Postfix Position Counter
let counter = 1;
let a = ++counter; // (*)
console.log(a); // 2
// Prefix Position Counter
let counter = 1;
let a = counter++; // (*) changed ++counter to counter++
console.log(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 the counter but returns the old value before being incremented.
Code Example:
// Prefix Position:
let counterOne = 5;
let a = ++counterOne; // (*)
console.log(a); // console.logs 6
// Postfix Position:
let counterTwo = 5;
let b = counterTwo++; // (*) changed ++counter to counter++
console.log(b); // console.logs 5
Can the increment and decrement 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 one-action in our code.
Proper Implementation:
// We advise a style of “one line – one action”:
let counter = 1;
console.log(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 occurs when multiple expressions are separated by commas within a single statement?
View Answer:
Interview Response: In JavaScript, separating expressions by a comma creates a comma-separated sequence of expressions. The expressions are evaluated left-to-right, and the result of the last expression is returned.
Code Example:
Here's an example to demonstrate the behavior of using commas to separate multiple expressions within a single statement:
let a = 1, b = 2, c = 3;
let result = (a++, b++, c++, a + b + c);
console.log(result); // Output: 7
console.log(a); // Output: 2
console.log(b); // Output: 3
console.log(c); // Output: 4
note
While this code demonstrates the evaluation and side effects of using commas in expressions, it's generally recommended to use separate statements or more explicit and readable code for better code maintainability.
What is the comma operator used for in JavaScript?
View Answer:
Interview Response: The comma operator in JavaScript is used to evaluate multiple expressions sequentially within a single statement, returning the result of the last expression.
Code Example:
let a = (1 + 2, 3 + 4);
console.log(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: The comma operator is useful for concise code, combining multiple operations in a single statement, like initializing multiple variables or running side effects in a loop. We should use the comma operator judiciously in JavaScript, as it can make code less readable.
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: In JavaScript, the comma operator has the lowest precedence among all operators, meaning it is evaluated after all other operators in an expression.
What is the mathematical order of operations in JavaScript?
View Answer:
Interview Response: The order of operations in JavaScript follows the standard PEMDAS rule: Parentheses, Exponents, Multiplication, and Division, from left to right, and Addition and Subtraction, from left to right.
What is the general order of operations in JavaScript?
View Answer:
Interview Response: In JavaScript, the general order of operations includes: Parentheses, member and computed member access, new and function calls, unary operators, arithmetic, comparison, logical, ternary, assignment, and finally, the comma operator.
What is NaN in JavaScript?
View Answer:
Interview Response: In JavaScript, NaN stands for Not a Number. It is a special value representing the result of an undefined or unrepresentative mathematical operation, such as the square root of a negative number or dividing zero by zero.
Code Example:
Here's a simple code example to demonstrate the NaN value in JavaScript.
let result = "Hello" / 5;
console.log(result); // Output: NaN
console.log(typeof result); // Output: "number"
console.log(Number.isNaN(result)); // Output: true
How do you perform exponentiation operation in JavaScript?
View Answer:
Interview Response: In JavaScript, we can use the Math.pow() method or the exponentiation operator with double asterisks for exponentiation.
Code Example:
let result1 = 2 ** 3;
console.log(result1); // Output: 8
let result2 = Math.pow(2, 3);
console.log(result2); // Output: 8
What happens when you divide zero by zero in JavaScript?
View Answer:
Interview Response: In JavaScript, dividing zero by zero results in NaN, a special value representing an undefined or unrepresentative mathematical operation.
Code Example:
Here's a code example that demonstrates the result of dividing zero by zero in JavaScript.
let result = 0 / 0;
console.log(result); // Output: NaN
console.log(typeof result); // Output: "number"
console.log(Number.isNaN(result)); // Output: true
How do you round a number to a specified number of decimal places in JavaScript?
View Answer:
Interview Response: To round a number to a specified number of decimal places in JavaScript, you can use the toFixed() method.
Code Example:
let num = 7.14159;
let roundedNum = num.toFixed(2);
console.log(roundedNum); // Output: "7.14"
note
Please note that the toFixed() method returns a string, so if you need a numeric value, you may need to convert it back using parseFloat() or Number().
What is the difference between equality and strict equality operators?
View Answer:
Interview Response: Equality (==) compares values, performing type coercion if needed. Strict equality (===) compares values and types, requiring both to match for true.
Code Example:
console.log(5 == "5"); // Output: true (Equality with type coercion)
console.log(5 === "5"); // Output: false (Strict equality without type coercion)
console.log(true == 1); // Output: true (Equality with type coercion)
console.log(true === 1); // Output: false (Strict equality without type coercion)