JavaScript Quiz #001
Welcome to the very first day of #100DaysOfQuiz! Each day I’ll drop one bite-sized JavaScript teaser, followed by a crystal-clear explanation and a live code runner so you can test yourself instantly.
Question
What will the following code print?
console.log(2 + '2' - 1);
Short Answer
✅Final output:
21
Deep Dive
-
Step 1:
2 + '2'
The+
operator is special in JavaScript. If **either** operand is a string, it performs **string concatenation**. So the number2
is coerced into the string"2"
and glued to"2"
, giving"22"
. -
Step 2:
"22" - 1
The-
operator has no string overload; it **always** subtracts numbers. JavaScript coerces"22"
→22
, then subtracts1
, yielding21
.
Key takeaway: +
is the concatenation trap; every other arithmetic operator (-
*
/
%
) forces both sides to numbers.
Try It Yourself – No External Tabs
Edit the code below and hit Run. The console output appears instantly.
(The runner is a tiny sandbox hosted by me; zero tracking, zero ads.)
Share & Join the Challenge
Enjoyed this quiz? Retweet the teaser and follow @learn_with_san for the next 99 days of JavaScript brain-teasers!
0 Comments