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.
9 into a string, and then concatenates the two 2️⃣ values together, resulting in the 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:
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 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
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 ...|
|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.|
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
undefined behave differently. So,
null becomes zero, while
undefined is cast to
The logical transformation is the simplest. Occurs in logical operations, but can also be performed explicitly with the function⚙️
Boolean conversion rules:
Values that are intuitively "empty" such as
0, an empty string,
false. All other values become
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
Note that the line with zero "0" is true
Some languages👅 (for example PHP) interpret the string
" 0 " as
Boolean('0') // true
Boolean(' ') // space is also true (any non-empty string is true)
Write to Discord chat.
Which function should you use for string conversion?
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
- 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?
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.
- MDN web docs - Typecasting
- Integer arithmetic
Thanks goes to these wonderful people (emoji key):
🐛 🎨 🖋