Unary operators help you change or check a single value quickly. They make your code shorter and easier to read.
Unary operators in Javascript
Introduction
Syntax
Javascript
++variable
--variable
+value
-value
!value
delete object.propertyUnary operators work with only one value.
Some unary operators change the value, others just check or convert it.
Examples
Javascript
let count = 5; count++; // count becomes 6
Javascript
let str = "123"; let num = +str; // num is 123 as a number
Javascript
let flag = true; let notFlag = !flag; // notFlag is false
Javascript
let obj = {a: 1}; delete obj.a; // obj is now {}
Sample Program
This program shows different unary operators: increment, decrement, unary plus, logical NOT, and delete.
Javascript
let x = 10; console.log(x); // 10 x++; console.log(x); // 11 x--; console.log(x); // 10 let str = "42"; console.log(+str); // 42 (number) let isTrue = false; console.log(!isTrue); // true let person = {name: "Anna", age: 25}; delete person.age; console.log(person); // { name: 'Anna' }
Important Notes
Increment (++) and decrement (--) operators change the variable's value by 1.
Unary plus (+) tries to convert a value to a number.
Logical NOT (!) flips true to false and false to true.
Delete removes a property from an object but does not affect variables.
Summary
Unary operators work with one value to change or check it.
They help write shorter and clearer code for simple tasks.
Common unary operators include ++, --, +, -, !, and delete.
Practice
1. Which of the following is a unary operator in JavaScript?
easy
Solution
Step 1: Understand unary operator definition
A unary operator works with only one operand (value).Step 2: Identify unary operators in options
++ increments a single variable by 1, so it is unary. += and && require two operands, === compares two values.Final Answer:
++ -> Option AQuick Check:
Unary operator = ++ [OK]
Hint: Unary operators work on one value only, like ++ or -- [OK]
Common Mistakes:
- Confusing += as unary instead of binary
- Thinking && is unary when it's binary
- Mixing comparison operators with unary
2. Which of the following is the correct syntax to decrement variable
x by 1 using a unary operator?easy
Solution
Step 1: Recall decrement unary operator syntax
The decrement operator is written as -- before or after the variable.Step 2: Check each option for correct syntax
x-- is valid and decreases x by 1. x-= is incomplete syntax. --x- and x--x are invalid.Final Answer:
x-- -> Option BQuick Check:
Decrement syntax = x-- [OK]
Hint: Decrement uses -- placed before or after variable [OK]
Common Mistakes:
- Using x-= instead of x--
- Adding extra characters like x--x
- Placing -- incorrectly like --x-
3. What is the output of the following code?
let a = 5;
console.log(-a);
medium
Solution
Step 1: Understand unary minus operator
The unary minus operator (-) changes the sign of the number.Step 2: Apply unary minus to variable a
a is 5, so -a becomes -5.Final Answer:
-5 -> Option AQuick Check:
Unary minus of 5 = -5 [OK]
Hint: Unary minus flips sign: positive to negative [OK]
Common Mistakes:
- Confusing unary minus with subtraction
- Expecting output 5 instead of -5
- Thinking it returns NaN or undefined
4. Find the error in this code snippet:
let count = 10;
count = ++;
medium
Solution
Step 1: Analyze the use of ++ operator
The ++ operator must be used with a variable, either before or after it.Step 2: Identify the error in the code
Here, ++ is used alone without a variable, causing a syntax error.Final Answer:
Missing variable after ++ operator -> Option CQuick Check:
++ needs a variable to work [OK]
Hint: ++ must be attached to a variable, not alone [OK]
Common Mistakes:
- Thinking ++ alone increments something
- Ignoring syntax errors from incomplete expressions
- Confusing missing semicolon as main error
5. Given the code below, what will be the value of
result?let x = 3;
let y = ++x + x++ + --x;
let result = y;
hard
Solution
Step 1: Evaluate ++x (pre-increment)
++x increases x from 3 to 4, returns 4.Step 2: Evaluate x++ (post-increment)
x++ returns current x (4), then increments x to 5.Step 3: Evaluate --x (pre-decrement)
--x decreases x from 5 to 4, returns 4.Step 4: Calculate y
y = 4 + 4 + 4 = 12.Step 5: Check final value of result
result = y = 12.Final Answer:
12 -> Option DQuick Check:
Pre/post increments affect value and order [OK]
Hint: Pre-increment changes value before use; post-increment after [OK]
Common Mistakes:
- Ignoring order of increments and decrements
- Mixing pre- and post-increment results
- Calculating without updating variable values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
