Operators in JavaScript: From Basics to Advanced Uses

Operators in JavaScript: From Basics to Advanced Uses image

FAQ

What are operators in JavaScript?

Operators in JavaScript are symbols or phrases that perform operations on one or more operands (values or variables). They are used to assign values, compare values, perform arithmetic operations, and more.

What is the difference between the == and === operators?

The == operator is used for abstract equality and will convert operands to the same type before making the comparison, potentially leading to true in cases where types are different. The === operator, on the other hand, is used for strict equality and will only return true if both the value and the type of the two operands are identical.

How can I concatenate strings in JavaScript?

You can concatenate strings using the + operator. For example, ‘Hello, ‘ + ‘world!’ would result in ‘Hello, world!’.

What are unary operators in JavaScript?

Unary operators are operators that operate on a single operand. Examples include the increment operator (++), which adds one to its operand, and the decrement operator (-), which subtracts one from its operand.

Can you use the spread operator (…) in JavaScript for object literals?

Yes, the spread operator (…) can be used in object literals to copy the properties from one object to another in a more succinct way. For example: let obj1 = {x: 1, y: 2}; let obj2 = {…obj1, z: 3}; // obj2 is now {x: 1, y: 2, z: 3}.

What is the purpose of the ternary operator in JavaScript?

The ternary operator is a shorthand way of writing a conditional statement. It takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is truthful, followed by a colon (:), and finally the expression to execute if the condition is false.

How do the prefix and postfix versions of ++ and - differ?

In prefix mode (++x or -x), the operation is performed, and the value is then returned. In postfix mode (x++ or x-), the value is returned first, and then the operation is performed. This difference affects the value returned by the expressions in which they are used.

What operator is used for string interpolation in JavaScript?

Template literals allow for string interpolation using the `${}` syntax within back-ticked strings (` `). For example: `Hello, ${name}!`.

Is there a nullish coalescing operator in JavaScript, and if so, what does it do?

Yes, the nullish coalescing operator (??) returns the right-hand operand when the left-hand operand is null or undefined, and otherwise returns the left-hand operand. It is useful for providing default values.

How can I delete a property from an object in JavaScript?

You can delete a property from an object using the delete operator. For example, if you have an object `let obj = {x: 1, y: 2};` and you want to remove the property `y`, you can use `delete obj.y;`.
Categories
JavaScript Foundations Variables, data types, and operators
We use cookies. If you continue to use the site, we will assume that you are satisfied with it.
I agree