Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

behaviour of + operator depends on types of operands #22

Open
michaelficarra opened this issue Dec 12, 2012 · 2 comments
Open

behaviour of + operator depends on types of operands #22

michaelficarra opened this issue Dec 12, 2012 · 2 comments

Comments

@michaelficarra
Copy link

+ can mean either addition or string concatenation depending on its operands. And if neither operand is a literal (a + b), it becomes a chore to figure out which behaviour is being denoted. In the most extreme cases, one can't even statically determine this. This leads to accidental addition being a common bug even among experienced ECMAScripters.

@DavidBruant
Copy link
Owner

What would be the associated good practice?
Always coerce to the expected type (string xor number) before using +?

Thanks for your contribution. I'm glad ECMAScript Regrets can be of some help in other projects ;-)

@michaelficarra
Copy link
Author

When intending to do string concatenation, start with an empty string literal. So a + b becomes "" + a + b. When intending to do addition, prefix each operand with a unary plus: +a + +b. That one may get a little tedious.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants