Advertisement
satishfrontenddev5

Untitled

Feb 18th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  how 2+ +'2'=4 in js?
  2.  
  3. ## 🗒️ Answer
  4. In JavaScript, the expression `2+ +'2'` evaluates to `4` due to type coercion and unary plus operator:
  5.  
  6. 1. The first `2` is a number.
  7. 2. The second `'2'` is a string.
  8. 3. The unary plus operator `+` before the second `'2'` coerces it into a number. Since it's a valid number representation, it becomes `2`.
  9. 4. Now the expression becomes `2 + 2`, which equals `4`.
  10.  
  11. 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