What exactly is Type Coercion in Javascript?

前端 未结 10 1101
后悔当初
后悔当初 2020-11-22 04:36

What exactly is type coercion in Javascript?

For example, on the use of == instead of ===?

相关标签:
10条回答
  • 2020-11-22 05:30

    If data type is not equal with each other then Coercion Happen. like 3 == "3" or boolen == integer

    0 讨论(0)
  • 2020-11-22 05:33

    Type coercion is the process of converting value from one type to another (such as string to number, object to boolean, and so on). Any type, be it primitive or an object, is a valid subject for type coercion. To recall, primitives are: number, string, boolean, null, undefined + Symbol (added in ES6).

    Implicit vs. explicit coercion Type coercion can be explicit and implicit.

    When a developer expresses the intention to convert between types by writing the appropriate code, like Number(value), it’s called explicit type coercion (or type casting).

    Since JavaScript is a weakly-typed language, values can also be converted between different types automatically, and it is called implicit type coercion. It usually happens when you apply operators to values of different types, like 1 == null, 2/’5', null + new Date(), or it can be triggered by the surrounding context, like with if (value) {…}, where value is coerced to boolean.

    One operator that does not trigger implicit type coercion is ===, which is called the strict equality operator. The loose equality operator == on the other hand does both comparison and type coercion if needed.

    Implicit type coercion is a double edge sword: it’s a great source of frustration and defects, but also a useful mechanism that allows us to write less code without losing the readability.

    Three types of conversion The first rule to know is there are only three types of conversion in JavaScript:

    • to string
    • to boolean
    • to number

    Secondly, conversion logic for primitives and objects works differently, but both primitives and objects can only be converted in those three ways.

    Let’s start with primitives first.

    String conversion

    To explicitly convert values to a string apply the String() function. Implicit coercion is triggered by the binary + operator, when any operand is a string:

    String(123) // explicit
    123 + ''    // implicit
    

    All primitive values are converted to strings naturally as you might expect:

    String(123)                   // '123'
    String(-12.3)                 // '-12.3'
    String(null)                  // 'null'
    String(undefined)             // 'undefined'
    String(true)                  // 'true'
    String(false)                 // 'false'
    

    Symbol conversion is a bit tricky, because it can only be converted explicitly, but not implicitly.

    String(Symbol('my symbol'))   // 'Symbol(my symbol)'
    '' + Symbol('my symbol')      // TypeError is thrown
    

    Boolean conversion

    To explicitly convert a value to a boolean apply the Boolean() function. Implicit conversion happens in logical context, or is triggered by logical operators ( || && !) .

    Boolean(2)          // explicit
    if (2) { ... }      // implicit due to logical context
    !!2                 // implicit due to logical operator
    2 || 'hello'        // implicit due to logical operator
    

    Note: Logical operators such as || and && do boolean conversions internally, but actually return the value of original operands, even if they are not boolean.

    // returns number 123, instead of returning true
    // 'hello' and 123 are still coerced to boolean internally to calculate the expression
    let x = 'hello' && 123;   // x === 123
    

    As soon as there are only 2 possible results of boolean conversion: true or false, it’s just easier to remember the list of falsy values.

    Boolean('')           // false
    Boolean(0)            // false     
    Boolean(-0)           // false
    Boolean(NaN)          // false
    Boolean(null)         // false
    Boolean(undefined)    // false
    Boolean(false)        // false
    

    Any value that is not in the list is converted to true, including object, function, Array, Date, user-defined type, and so on. Symbols are truthy values. Empty object and arrays are truthy values as well:

    Boolean({})             // true
    Boolean([])             // true
    Boolean(Symbol())       // true
    !!Symbol()              // true
    Boolean(function() {})  // true
    

    Numeric conversion

    For an explicit conversion just apply the Number() function, same as you did with Boolean() and String() .

    Implicit conversion is tricky, because it’s triggered in more cases:

    • comparison operators (>, <, <=,>=)

    • bitwise operators ( | & ^ ~)

    • arithmetic operators (- + * / % ). Note, that binary+ does not trigger numeric conversion, when any operand is a string.

    • unary + operator

    • loose equality operator == (incl. !=).

      Note that == does not trigger numeric conversion when both operands are strings.

      Number('123') // explicit +'123' // implicit 123 != '456' // implicit 4 > '5' // implicit 5/null // implicit true | 0 // implicit

    Here is how primitive values are converted to numbers:

    Number(null)                   // 0
    Number(undefined)              // NaN
    Number(true)                   // 1
    Number(false)                  // 0
    Number(" 12 ")                 // 12
    Number("-12.34")               // -12.34
    Number("\n")                   // 0
    Number(" 12s ")                // NaN
    Number(123)                    // 123
    
    0 讨论(0)
  • 2020-11-22 05:33

    Type coercion is the process of converting value from one type to another (such as string to number, object to boolean, and so on). Any type, be it primitive or an object, is a valid subject for type coercion. To recall, primitives are: number, string, boolean, null, undefined + Symbol (added in ES6).

    Type coercion can be explicit and implicit.

    When a developer expresses the intention to convert between types by writing the appropriate code, like Number(value), it’s called explicit type coercion (or type casting).

    Since JavaScript is a weakly-typed language, values can also be converted between different types automatically, and it is called implicit type coercion. It usually happens when you apply operators to values of different types, like 1 == null, 2/’5', null + new Date(), or it can be triggered by the surrounding context, like with if (value) {…}, where value is coerced to boolean.

    here is some example for implicit type coercion:

    true + false
    12 / "6"
    "number" + 15 + 3
    15 + 3 + "number"
    [1] > null
    "foo" + + "bar"
    'true' == true
    false == 'false'
    null == ''
    !!"false" == !!"true"
    [‘x’] == ‘x’
    [] + null + 1
    [1,2,3] == [1,2,3]
    {}+[]+{}+[1]
    !+[]+[]+![]
    new Date(0) - 0
    new Date(0) + 0
    

    read more: https://www.freecodecamp.org/news/js-type-coercion-explained-27ba3d9a2839/

    0 讨论(0)
  • 2020-11-22 05:37

    Let’s start with a short intro to type systems which I think will help you understand the general idea of type coercion.

    The type system of a language defines rules that tell us what types of data exist in that language and how they can be combined using different operators. For example, one such rule might specify that the plus (+) operator only acts on numbers. These rules exist primarily to prevent you from shooting yourself in the foot. But what happens when the programmer breaks that rule in the program? There’s nothing preventing the programmer from typing {} + {} or “hello” + 5 in a program even if the language doesn’t think those expressions make any sense.

    What ultimately happens in those situations depends on how strict the language is about its type rules.

    A languages type system often holds one of two positions about you breaking its rules:

    1. Say “Hey, that’s not cool!” and immediately crash your program.
    2. Say “I can’t do anything with {} … but I can do something with numbers” and attempt to convert {} to a number.

    Languages with type systems that take the first position about its rules are colloquially referred to as “strongly typed” languages. They are strict about not letting you break its rules. Those that take the second approach (such as JavaScript) are referred to as “weakly typed” or “loosely typed” languages. Sure, you can break the rules, but don’t be surprised when it converts the type of data you described in your program by force in order to comply with its rules. That behavior is known as … (drum roll) ... type coercion.

    Now let's look at some examples in JavaScript. First, let's start with an expression that does not lead to type coercion.

    5 + 5
    

    Using the + operator with two numbers which is perfectly valid. The program will treat + to mean “add” and happily add the two numbers. No conversion necessary.

    But what about …

    [] + 5
    

    Uh oh. In JavaScript, + can mean add two numbers or concatenate two strings. In this case, we have neither two numbers nor two strings. We only have one number and an object. According to JavaScript's type rules, this makes no logical sense. Since it’s forgiving about you breaking its rules, instead of crashing it tries to make sense of it anyway. So what does JavaScript do? Well, it knows how to concatenate strings, so it converts both [] and 5 into strings and the result is string value “5”.

    What’s the deal with the comparison operators == and ===? Why are there two comparison operators?

    == is not immune to JavaScript’s type conversion behavior. Expressions such as 5 == “5” will evaluate to true because JavaScript will attempt to convert one of them so that it’s comparing the same type of data.

    In many cases, that’s not desirable because you probably want to know if some data you’re comparing against is of a different type so that you can decide what to do about it. That’s where the === operator comes in. When you use ===, no type conversion will take place. Therefore, the expression 5 === “5” will evaluate to false.

    0 讨论(0)
提交回复
热议问题