Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- how 2+ +'2'=4 in js?
- ## 🗒️ Answer
- In JavaScript, the expression `2+ +'2'` evaluates to `4` due to type coercion and unary plus operator:
- 1. The first `2` is a number.
- 2. The second `'2'` is a string.
- 3. The unary plus operator `+` before the second `'2'` coerces it into a number. Since it's a valid number representation, it becomes `2`.
- 4. Now the expression becomes `2 + 2`, which equals `4`.
- So, `2+ +'2'` evaluates to `4` because JavaScript coerces the string `'2'` to a number using the unary plus operator[[1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement