0 of 8 problems solved • 0%
Use in a course

Types and Conversion

8 exercisesEasy

JavaScript values have types even though variables do not need type annotations. Knowing whether a value is text, a number, or a boolean helps you predict how operators will behave.

const quantityText = "4"; const quantity = Number(quantityText); console.log(typeof quantityText); // "string" console.log(quantity + 1); // 5

Inspect values with typeof, convert deliberately with Number and String, and recognise NaN when a conversion fails. Explicit conversions keep inputs such as form fields from producing surprising results.