JS Built-ins
Numberbeginner

Number() β€” converting strings to numbers

πŸ“– Usage Example
// Convert a value to a number
Number('42')    // β†’ 42
Number(true)    // β†’ 1
Number(false)   // β†’ 0
Number('')      // β†’ 0

Use the Number() function to explicitly convert a value to a numeric type, which is more predictable than implicit coercion.

Number() Constructor

Number(value) converts a value to a number primitive.

Challenge: Implement strToNum(str) that converts a string to a number using Number().

strToNum('42')    // β†’ 42
strToNum('3.14')  // β†’ 3.14
πŸ’‘ Hints (1)
  • β€’ `Number(str)` parses numeric strings directly.