convert string to number node.js

user3488862 picture user3488862 · May 17, 2016 · Viewed 150.8k times · Source

I'm trying to convert req.params to Number because that is what I defined in my schema for year param.

I have tried

req.params.year = parseInt( req.params.year, 10 );  

and

Number( req.params.year);

and

1*req.params.year;

but non of them works. Do I need to install something?

Answer

Tusk picture Tusk · May 17, 2016

You do not have to install something.

parseInt(req.params.year, 10);

should work properly.

console.log(typeof parseInt(req.params.year)); // returns 'number'

What is your output, if you use parseInt? is it still a string?