Type Coercion

Published on: August 26, 2016

Welcome to the tenth Easy JavaScript Tutorial, Beginner JavaScript Tutorial, part of Easy Programming! This tutorial covers Type Coercion. What is type coercion? Why should you use it? Learn about it in this video!

I mentioned in the intro video that JavaScript is a weakly typed language. It means that the variable type can be changed without any major issues. 

To coerce a string to become a number, we can use either parseInt or parseFloat, the difference being that parseFloat will accept decimals or floating points, whereas parseInt will only take the integer. 

Rules of parseInt and parseFloat:

  1. Only the first number in a string is parsed.
  2. Although Spaces are allowed, only the first number will be parsed.
  3. If the first character can't be coerced, then it can't be converted!

To fork the fiddle and follow along: https://jsfiddle.net/easyjs/ex71ghux/

Remember to checkout the Resources section below for associated downloadable content, JSFiddle links, and other resources. Watch the video and follow along!

Resources:



Comments: