DEV Community

Chan
Chan

Posted on

Type Conversion in Javascript

Converting to string

String()

It can take null and undefined as arguments without error.

String(null); // 'null'
String(undefined); // 'undefined'
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more