Bird
Raised Fist0
Javascriptprogramming~10 mins

Unary operators in Javascript - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to increase the value of count by 1 using a unary operator.

Javascript
let count = 5;
count[1];
console.log(count);
Drag options to blanks, or click blank then click option'
A-
B++
C+=
D--
Attempts:
3 left
💡 Hint
Common Mistakes
Using '--' which decreases the value.
Using '+=' which is not a unary operator.
Using '-' which subtracts but is not unary increment.
2fill in blank
medium

Complete the code to convert the string numStr to a number using a unary operator.

Javascript
let numStr = "42";
let num = [1]numStr;
console.log(typeof num);
Drag options to blanks, or click blank then click option'
A+
BNumber
CparseInt
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using parseInt which is a function, not a unary operator.
Using Number which is a function, not a unary operator.
Using '-' which negates the number but does not convert string to number.
3fill in blank
hard

Fix the error in the code by choosing the correct unary operator to negate the value of value.

Javascript
let value = 10;
let negated = [1]value;
console.log(negated);
Drag options to blanks, or click blank then click option'
A-
B+
C++
D--
Attempts:
3 left
💡 Hint
Common Mistakes
Using '++' which increments the value.
Using '+' which does not change the sign.
Using '--' which decrements the value.
4fill in blank
hard

Fill both blanks to create an object flags where keys are numbers and values are their negations using unary operators.

Javascript
const numbers = [1, 2, 3];
const flags = {};
for (const num of numbers) {
  flags[[1]] = [2];
}
console.log(flags);
Drag options to blanks, or click blank then click option'
Anum
B-num
Cnum.toString()
Dnum * -1
Attempts:
3 left
💡 Hint
Common Mistakes
Using number directly as key without converting to string.
Using multiplication instead of unary minus for negation.
Using 'num' as value which is not negated.
5fill in blank
hard

Fill both blanks to create a new array results where each element is the negation of the numeric conversion of strings in arr.

Javascript
const arr = ["5", "-3", "10"];
const results = arr.map(item => [1][2]item);
console.log(results);
Drag options to blanks, or click blank then click option'
A-
B+
D!
Attempts:
3 left
💡 Hint
Common Mistakes
Using logical NOT ! which converts to boolean.
Not converting string to number before negation.
Placing operators incorrectly causing syntax errors.

Practice

(1/5)
1. Which of the following is a unary operator in JavaScript?
easy
A. ++
B. +=
C. &&
D. ===

Solution

  1. Step 1: Understand unary operator definition

    A unary operator works with only one operand (value).
  2. Step 2: Identify unary operators in options

    ++ increments a single variable by 1, so it is unary. += and && require two operands, === compares two values.
  3. Final Answer:

    ++ -> Option A
  4. Quick 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
A. --x-
B. x--
C. x-=
D. x--x

Solution

  1. Step 1: Recall decrement unary operator syntax

    The decrement operator is written as -- before or after the variable.
  2. 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.
  3. Final Answer:

    x-- -> Option B
  4. Quick 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
A. -5
B. 5
C. NaN
D. undefined

Solution

  1. Step 1: Understand unary minus operator

    The unary minus operator (-) changes the sign of the number.
  2. Step 2: Apply unary minus to variable a

    a is 5, so -a becomes -5.
  3. Final Answer:

    -5 -> Option A
  4. Quick 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
A. Syntax error due to missing semicolon
B. Incorrect use of decrement operator
C. Missing variable after ++ operator
D. No error, code runs fine

Solution

  1. Step 1: Analyze the use of ++ operator

    The ++ operator must be used with a variable, either before or after it.
  2. Step 2: Identify the error in the code

    Here, ++ is used alone without a variable, causing a syntax error.
  3. Final Answer:

    Missing variable after ++ operator -> Option C
  4. Quick 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
A. 10
B. 13
C. 11
D. 12

Solution

  1. Step 1: Evaluate ++x (pre-increment)

    ++x increases x from 3 to 4, returns 4.
  2. Step 2: Evaluate x++ (post-increment)

    x++ returns current x (4), then increments x to 5.
  3. Step 3: Evaluate --x (pre-decrement)

    --x decreases x from 5 to 4, returns 4.
  4. Step 4: Calculate y

    y = 4 + 4 + 4 = 12.
  5. Step 5: Check final value of result

    result = y = 12.
  6. Final Answer:

    12 -> Option D
  7. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard