Theoretical recap.
In JavaScript, every value has a type: string, number, Boolean, undefined, null, and others.
Type conversion or type casting means transforming a value from one type into another.
There are two kinds.
Explicit conversion is when you deliberately convert a value using functions like number, string, Boolean, parseInt, or parseInt.
Implicit conversion, coercion, happens automatically when JavaScript tries to reconcile mismatched types.
For example, when you use plus between a string and a number.
Mental model.
Think of each value as an object wearing a type label.
Conversion swaps that label and sometimes reshapes the content.
For instance, number forty-two returns the number forty-two, while string forty-two returns the string forty-two.
Common mistakes.
Confusing equals equals, loose coerces types, with equals equals equals, strict, no coercion.
Expecting five plus two to equal seven, it returns fifty-two because plus concatenates when a string is involved.
Forgetting that number ABC returns NaN, not an error, and assuming parseInt "12px" fails, it actually returns twelve.
Mini example.
Let age equals thirty.
Console.log type of age, slash slash string.
Let age number equals number age.
Console.log type of age number, slash slash number.
Console.log age number plus five, slash slash thirty-five.
Keep this recap open as a reference while you work.
Read the full transcript.
Create an account to read the whole episode, search across every transcript, and follow the shows you care about.
Theoretical recap.
In JavaScript, every value has a type: string, number, Boolean, undefined, null, and others.
Type conversion or type casting means transforming a value from one type into another.
There are two kinds.
Explicit conversion is when you deliberately convert a value using functions like number, string, Boolean, parseInt, or parseInt.
Implicit conversion, coercion, happens automatically when JavaScript tries to reconcile mismatched types.
For example, when you use plus between a string and a number.
Mental model.
Think of each value as an object wearing a type label.
Conversion swaps that label and sometimes reshapes the content.
For instance, number forty-two returns the number forty-two, while string forty-two returns the string forty-two.
Common mistakes.
Confusing equals equals, loose coerces types, with equals equals equals, strict, no coercion.
Expecting five plus two to equal seven, it returns fifty-two because plus concatenates when a string is involved.
Forgetting that number ABC returns NaN, not an error, and assuming parseInt "12px" fails, it actually returns twelve.
Mini example.
Let age equals thirty.
Console.log type of age, slash slash string.
Let age number equals number age.
Console.log type of age number, slash slash number.
Console.log age number plus five, slash slash thirty-five.
Keep this recap open as a reference while you work.