4

There is a line of code in angular2.

this.id = +this.route.snapshot.params['id'];

What is the "+" means before "this.route.snapshot.params['id']"?

I also see "+" added before a folder name such as "+detail" in angular2-webpack-starter.

Do they have the same meaning?

1
  • 2
    + is used for a quick changing type of number-ish strings into number, e.g. +"5" will return 5. Commented Mar 7, 2017 at 21:14

2 Answers 2

11

Using + in Javascript is a quick way to cast a string to a number as long as the string is already in the form of an integer or float.

+'5000' // yields 5000
+'2.5'  // yields 2.5

If the string contains any character that is not an integer (or decimal in the case of a float), this method will return NaN.

+'5n'  // yields NaN
+'abcd'  // yields NaN
Sign up to request clarification or add additional context in comments.

2 Comments

Not necessarily integers. You could have something like +"2.5".
Thanks for catching my mistake! I update the answer to be more accurate.
1

enter image description here

In short if you put + operator in front of string which contains Number then always get number other than that you will get NaN.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.