• in
InterCourses
CoursesBlogs
0
← Introduction to JavaScript
○What is JavaScript?○Your First JavaScript Code○Data Types○Type Checking
○Declaring Variables○let and const in Practice○Operators●Type Conversion
○Conditionals○Switch Statement○For Loops○While Loop○Break and Continue
○Defining Functions○Arrow Functions○Scope○Closures○Project - Calculator○Function Expressions
○Arrays○Array Mutation Methods○Array Search and Slice○Array Iteration Methods○Multidimensional Arrays○Destructuring○Array Reverse and Sort○Array Fill○Array Find and Includes○Array Flat and FlatMap○Array Reduce, Some, and Every
○Objects○Object Methods and Destructuring○JSON○Math and Date○Date Basics
○02 Template Literals○String Methods○Regular Expressions
○Higher-Order Functions○Callbacks○map, filter, and reduce○Recursion○Project - Data Transform Pipeline
○OOP Introduction○Classes○Inheritance○Encapsulation
○Asynchronous JavaScript○Promises and Async Await○Async API Simulation

Type Conversion

Sometimes you need to change a value from one type to another. JavaScript supports both explicit (manual) and implicit (automatic) type conversion.

Explicit conversion

ConversionMethodExampleResult
To numberNumber()Number("42")42
parseInt()parseInt("3.9")3
parseFloat()parseFloat("3.9")3.9
Unary ++"42"42
To stringString()String(42)"42"
.toString()(42).toString()"42"
To booleanBoolean()Boolean(0)false
javascript
Number("100")   // 100
Number("")      // 0
Number("abc")   // NaN
Number(true)    // 1
Number(false)   // 0
Number(null)    // 0

Boolean(0)        // false
Boolean("")       // false
Boolean(null)     // false
Boolean("hello")  // true
Boolean(1)        // true

Implicit conversion (coercion)

JavaScript automatically converts types in certain contexts — sometimes in surprising ways:

javascript
"5" + 3      // "53"  — 3 converted to string
"5" - 3      // 2     — "5" converted to number
"5" * "3"    // 15    — both converted to numbers
true + 1     // 2     — true → 1
false + 1    // 1     — false → 0
null + 1     // 1     — null → 0
undefined + 1 // NaN

Your Task

  1. Declare a variable priceStr with the string value "29".
  2. Convert priceStr to a number and store it in a variable price.
  3. Declare a variable quantity with the number value 3.
  4. Calculate total as price * quantity.
  5. Display in #output: "Total: $87" (use String() or a template literal for the final message).
Hint 1
1 / 3
HINT 1

Use Number() to convert a string to a number.

Loading editor…
READY
intercourses
javascript