143

What exactly is type coercion in Javascript?

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

nbro
  • 15,395
  • 32
  • 113
  • 196
gespinha
  • 7,968
  • 16
  • 57
  • 91
  • 25
    `(true == 1) => true` / `(true === 1) => false`. – VisioN Nov 11 '13 at 20:53
  • 12
    @VisioN your comment doesn't help at all, I am asking: "why" does that happen? – gespinha Nov 11 '13 at 21:02
  • 5
    It happens so because JavaScript was developed in this way. My comment should answer your main question: *What exactly is Type Coercion in Javascript?* – VisioN Nov 11 '13 at 21:06
  • 9
    Via YDJS: "Converting a value from one type to another is often called "type casting," when done explicitly, and "coercion" when done implicitly (forced by the rules of how a value is used)." - https://github.com/getify/You-Dont-Know-JS/blob/master/types%20&%20grammar/ch4.md – mattLummus Jun 16 '15 at 18:31
  • If you’re looking for a **complete list** of all instances of type coercion in JavaScript, see the specification section on [Type Conversion](//tc39.es/ecma262/#sec-type-conversion), go through each abstract operation, hover over the subsection titles, click the [references](//i.stack.imgur.com/MXB01.png). As of ECMAScript 2023, there are 709 instances of type coercion throughout the spec, 243 of which are to string. – Sebastian Simon Dec 28 '22 at 16:48

9 Answers9

203

Type coercion means that when the operands of an operator are different types, one of them will be converted to an "equivalent" value of the other operand's type. For instance, if you do:

boolean == integer

the boolean operand will be converted to an integer: false becomes 0, true becomes 1. Then the two values are compared.

However, if you use the non-converting comparison operator ===, no such conversion occurs. When the operands are of different types, this operator returns false, and only compares the values when they're of the same type.

Coercion isn't only done by comparison operators, although they're the only ones that have both "strict" and "loose" variants. Most arithmetic operators will automatically converse non-numeric arguments to numbers, e.g. "50" / 5 is treated as 50 / 5. There are also many built-in functions and methods that require string arguments; if you give them something else, they'll automatically coerce them to strings.

But be careful -- + is both the arithmetic addition operator and the string concatenation operator -- if you do string + number, it converts the number to a string and concatenates, rather than converting the string to a number and adding. This is the source of many errors made when performing arithmetic on user input, since input is a string unless you explicitly convert it.

You can find a good explanation of JavaScript's coercion rules in You Don't Know JS and more reference-oriented documentation in MDN.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    How can I put this on a practical situation? Shouldn't I always use `===` when I want to compare if a value is equal to another? – gespinha Nov 11 '13 at 21:04
  • 1
    It depends on what you're doing. See the linked question. – Barmar Nov 11 '13 at 21:06
  • 8
    @GEspinha well that's the "art" of using a loosely typed language. Some people think so, and in general think loosely typed languages are the scourge of the programming world. However, if you know what you are doing, it can make for shorter, more flexible code. – CrayonViolent Nov 11 '13 at 21:06
  • Thanks guys, I will search on this matter to further understand the use of type coercion ;) – gespinha Nov 11 '13 at 21:10
  • 2
    @Barmar Does it also applies to `> , <` ? – Royi Namir Oct 12 '14 at 11:06
  • 1
    @RoyiNamir Yes. Whenever values of different types are combined, one of them is coerced to the type of the other unless you're using a strict operator. – Barmar Oct 12 '14 at 16:09
  • 1
    @Barmar what determines which operand type is converted? Does javascript convert the left side operand type to same as right operand when using == ? – blue-sky Mar 26 '16 at 09:01
  • @blue-sky The order doesn't matter, just the operator and the types of the two arguments. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators. If one of the operands is a boolean and the other isn't, the boolean is converted to 1 if true, 0 if false. – Barmar Mar 27 '16 at 03:49
  • This explains just a part of _type coercision_, but this [answer here](http://stackoverflow.com/a/38258318/1618202), lengthy as it is, covers it well. Basically, not only does `==` operator coerce but `+`, `/'`, etc may coerce operands into a different type e.g. `"50" + 5`, `20 / "2"`, etc. – ProfNandaa Mar 01 '17 at 03:22
  • This response is short, loud and clear, well explained how to use === – Saymon Oct 05 '17 at 20:44
  • @hlovdal I've added the link to the answer. – Barmar Apr 04 '21 at 14:08
  • Have to laugh at the guy who said people scoff at loosely typed languages but if you know what you’re doing it’s great…. in 2013. And now we have TS. An overly engineered way to avoid this. Lol. But yeah type coercion only happens when you use “==“. Another example would be “string”==string – Robert O'Toole May 25 '22 at 23:02
  • @ProfNandaa That's why I said "for instance". It wasn't meant to imply that only `==` does coercion. – Barmar May 25 '22 at 23:06
70

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.

linstantnoodles
  • 4,350
  • 1
  • 22
  • 24
  • 7
    thanks for nice explanation, especially for "A languages type system often holds one of two positions" – Humoyun Ahmad Feb 18 '17 at 07:16
  • 3
    This should be the accepted answer, as it shows automatic type coercion in multiple facets not just the == comparison example. This answer does a much better overall job of answering the question and removing all ambiguity. Thank you for taking the time to write it out. – the chad Jul 18 '18 at 18:42
8

In Python if you try to add, say, strings and integers, you get an error:

>>> "hi" + 10
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects

Yet in JavaScript, you don't. The 10 gets converted to a string:

> "hi" + 10
"hi10"

"Type coercion" is just a fancy misnomer for the above. In actuality, neither language has "types" in the sense of Java or C or other languages with static type systems. How the languages treat interactions between the various non-statically-typed values is a matter of choice and convention.

Claudiu
  • 224,032
  • 165
  • 485
  • 680
  • 1
    I think there is a bit of a problem with the example you have taken. What you suggested as an example for JS, works flawless with Java and C#. So, according to this answer, if one concludes that Java and C# support *type coercion* that won't be entirely true... – Romeo Sierra Dec 11 '18 at 03:18
4

let me explain type coercion with the following example

Type Coercion means Javascript automatically (on-the-fly) converts a variable from one datatype to another

Ex: 123 + "4" generally raises an error but in Javascript due to type coercion, it results in 1234 a string

if(23 == "23"){
    console.log(" this line is inside the loop and is executed ");
}

In the above code, because of type coercion - JavaScript thinks 23 (number) and "23" (string) are the same thing. this makes the condition true and prints the console.log

In the other case

if(23 === "23"){
   console.log(" this line is inside the loop and is NOT executed");
}

In === case Javascript doesn't do Type Coercion, and since 23 is a number and "23" is String and because of === these two datatypes are different and that leads to the false in condition. It does not print the console.log

In simple words

In this case = it is an assignment operator - which assigns values such as var a = 3;, etc

(below operators are for comparison)

In this case == Javascript converts/coerces the datatype to another and then compares it.

In this case === Javascript doesn't convert/coerces the datatype

In order to avoid bugs and for debugging purposes === is mostly used

Please let me know the accuracy of the above information.

PRagh
  • 92
  • 5
3

What is coercion:

Type coercion in javascript occurs when the Javascript engine has to perform a certain operation for which it needs data to be in a certain type. When the engine encounters data in a certain type that is not applicable for the operation it then coerces the data into a certain type. This is needed because variables in javascript are dynamically typed, which means that a given variable can be assigned a value of any type.

Example:


if(1){
  // 1 gets coerced to true
}


if(4 > '3') {
  // 3 gets coerced into a number
}


44 == "44"  // true, the string 44 gets converted to a nr

Boolean coercion:

In javascript coercion, all values are converted to true except for the following values which are coerced to false:

console.log(!!"");         // false
console.log(!!0);          // false
console.log(!!null);       // false
console.log(!!undefined);  // false
console.log(!!NaN);        // false
console.log(!!false);      // false

Also notice that in the above example that the double ! operator is used. The ! mark operator coerces a value into a boolean with the opposite value. We can use this operator twice to convert any value into a boolean.

Willem van der Veen
  • 33,665
  • 16
  • 190
  • 155
2

a == b means javascript will evaluate a against b based on if the values can be evaluated equally. For example, false == 0 will evaluate true because 0 is also the value of Boolean false. However, false === 0 will evaluate false because strictly comparing, 0 is not the same physical value as false. Another example is false == '' So basically loose comparison vs. strict comparison, because javascript is a loosely typed language. That is to say, javascript will attempt to convert the variable based on the context of the code, and this has the effect of making things equal if they are not strictly compared. php also has this behavior.

CrayonViolent
  • 32,111
  • 5
  • 56
  • 79
  • `0 is not the same physical value as false`. IMO physically `false` is exactly `0` in memory. I'd rather say they are different by type, since `false` is boolean, whereas `0` is integer. – VisioN Nov 11 '13 at 21:02
0
var str = 'dude';
console.log(typeof str); // "string"
console.log(!str); // false
console.log(typeof !str); // "boolean"

Example of a variable which is initially declared as a string being coerced into boolean value with the ! operator

Mister P
  • 1,253
  • 1
  • 11
  • 9
  • 3
    Please elaborate your answer. Code only answers are not really helpful. – cezar Nov 04 '17 at 20:02
  • 3
    personally I find code only examples succinct, self explanatory and very useful, I guess it is a matter of personal opinion – Mister P Nov 05 '17 at 16:58
0

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/

-5

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

A.S.Abir
  • 93
  • 7