Type conversion and casting
Type coercion (type coercion)
It is an automatic or implicit conversion of values from one data type to another (for example, a string to a number). Type conversions are similar to type conversions because they both convert values from one data type to another, with one key difference - type conversion is implicit, while type conversion can be implicit or explicit.
Examples :
In the example above, JavaScript casts the number 9
into a string, and then concatenates the two 2️⃣ values together, resulting in the string 59
. JavaScript had a choice between string or number and chose to use string.
The compiler could convert the string 5
to a number and return the sum of 14
, but it did not. To get this result, you need to explicitly convert the string 5
to a number using the Number()
method:
Type conversion
Means the transfer of data from one data type to another. Implicit conversion occurs when the compiler automatically assigns (assigns) data types, but the source code may also explicitly require the conversion to complete.
String conversion
String conversion occurs when you want to represent something as a string. For example, we can use the String (value)
function to convert a value to a string :
The transformation takes place in an obvious way. true
becomes"true"
Numeric conversion
Numerical conversion occurs in math functions and expressions.
We can use the Number (value)
function to explicitly convert value
to a number :
Explicit conversion is often used when we expect to get a number from a string context, such as text fields in forms.
If the string cannot be explicitly cast to a number, then the conversion result will be NaN
(Not-a-Number, "not a number "). For example :
Numeric conversion rules:
Value | Converted to ... |
---|---|
undefined | NaN |
null | 0 |
true / false | 1 / 0 |
string | White space is trimmed at the edges. Further, if an empty string remains, then we get 0, otherwise a number is "read" from a non-empty string. On error, the result is NaN. |
Examples:
Number(' 123 ') // 123
Number('123z') // NaN (error reading the number in place of the "z" character)
Number(true) // 1
Number(false) // 0
Number(null) // 0
Number(undefined) // NaN
Note that null
and undefined
behave differently. So, null
becomes zero, while undefined
is cast to NaN
.
Boolean conversion
The logical transformation is the simplest. Occurs in logical operations, but can also be performed explicitly with the function⚙️ Boolean (value)
.
Boolean conversion rules:
Values that are intuitively "empty" such as 0
, an empty string, null
, undefined
, and NaN
become false
. All other values become true
.
Boolean(1) // true
Boolean(0) // false
Boolean('Привет!') // true
Boolean('') // false
The shorter way of the Boolean
function, the double NOT (!!) is used to convert values to a boolean type:
!!'non-empty string' // true
!!null // false
Some languages (for example PHP) interpret the string " 0 "
as false
. But in JavaScript, if a string is not empty, then it is always true
Boolean('0') // true
Boolean(' ') // space is also true (any non-empty string is true)
Problems?
Write to Discord chat.
Questions:
Which function should you use for string conversion?
String (value)
Boolean (value)
Number (value)
What is typecasting?
- Transferring data from one type to another
- Converting values from one data type to another
- Representation of something as a string
What is the key difference between typecasting and typecasting?
- Type casting is explicit, and type conversion is implicit
- Type casting is implicit, and type conversion is explicit
- Type conversion is implicit, and type conversion can be both explicit and implicit
When would the conversion result be NaN
?
- When a string cannot be explicitly cast to a number
- When a number cannot be explicitly cast to a string
- When there is an error in the code
What do “empty” values become when converted?
null
true
false
In order to understand how much you learned this lesson, take the test in the mobile application of our school on this topic or in our telegram bot.
Links
- MDN web docs - Typecasting
- Code for Teens: The Perfect Beginner's Guide to Programming, Volume 1: Javascript - Jeremy Moritz
- JavaScript.ru
- Integer arithmetic
Contributors ✨
Thanks goes to these wonderful people (emoji key):
Dmitriy Vasilev 💵 | Resoner2005 🐛 🎨 🖋 |