Converting to string
String()
It can take null
and undefined
as arguments without error.
String(null); // 'null'
String(undefined); // 'undefined'
toString()
It throws an error if the variable is set to null
or undefined
.
null.toString() // Uncaught TypeError: Cannot read properties of null (reading 'toString')
undefined.toString() // Uncaught TypeError: Cannot read properties of null (reading 'toString')
Converting to Number
Number()
This constructor converts an argument to a number.
If the characters in the argument can't be interpreted as a number, it converts the argument to NaN
Number('123px'); // NaN
Number('$123'); // NaN
parseInt() or parseFloat()
It reads the number until it reaches the character that can't be interpreted as a number.
parseInt('12px'); // 12
parseint('$12'); // NaN
Top comments (0)