Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a + before a string means in javascript

Tags:

javascript

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?

like image 413
Henry Avatar asked Sep 20 '25 15:09

Henry


1 Answers

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
like image 115
Lance Jernigan Avatar answered Sep 22 '25 08:09

Lance Jernigan