Questions tagged [type-coercion]

In weakly typed languages, type coercion is where a data type is converted by the interpreter from one type to another (such as a string to an integer). This most often occurs in comparing two values. Use for questions where type coercion is an issue with your code.

Type coercion occurs in weakly typed languages (like or ). Consider this PHP code

$x = '1'; // string
$y = $x + 1;
echo $y;

$x is a string, but we call it like an integer when setting $y. So the PHP engine coerces '1' (which is a string data type) into an integer for the purposes of math. As such, the script echoes 2 because the string 1 can be coerced neatly into integer 1. But not everything is so neatly handled

$x = 'a';
$y = $x + 1;
var_dump($y);

Now, 'a' is not readily coerced into a number. As such, we get back integer(1) because PHP coerces strings to 0 (modern PHP emits a E_WARNING that this is happening).

This most often rears itself in doing comparisons. Consider this code

$x = 'Something';
echo (strpos($x, 'Some') != false) ? 'Found it!' : 'Did not find it';

This script doesn't run like we expect. It tells us it didn't find it when we know it should have found it. The problem is that strpos can return a boolean false or the position of where it found the match. In this case, the match was at position 0. Our test then becomes

if( 0 == false )

Type coercion nails us here because 0 is coerced into false in PHP. PHP and Javascript both have a workaround for this, however. We can use === and !== to tell the interpreter to not coerce before comparing. If we tell PHP not to coerce the 0, we get back the results we expect

echo (strpos($x, 'Some') !== false) ? 'Found it!' : 'Did not find it';
108 questions
0
votes
0 answers

When I am testing for truthiness, why is my Object.create(null) value true while null is false?

I am trying to confirm for myself whether a null object is false. First, I tried to create a null object using Object.create(null). Then I decided to test this object against null. I wrote a snippet of code to check for truthiness: let emptyObject =…
0
votes
0 answers

How to use GraphQL with msgpack APIs?

In modern msgpack APIs, it seems to be the norm to return identifiers as raw binary data; i.e. as Buffer objects in JavaScript-land. This seems to cause problems with the ID type as used in GraphQL schemas- GraphQL query error TypeError: ID cannot…
pospi
  • 3,210
  • 3
  • 23
  • 25
0
votes
2 answers

In JavaScript, what is the scope of a function declared within the condition of an if statement?

I recently had an interview question where I was shown this block of code (without the answer) and asked what would be printed to the console: var i = 1; if(function f(){}) { i += typeof f; } console.log(i); // prints "1undefined" I understand…
0
votes
1 answer

Bug in Javascript type coercion

I'm having this strange bug with Javascript and type coercion (automatic conversion of variables' type made by Javacript). Here is the code console.log('23' < '3'); that is inside a file called index.js that is invoked by this simple html
0
votes
1 answer

Explain this type-coercion in JavaScript

I have read that when we compare object with a number type-coercion happens. ToPrimitive on object gets called which then calls valueOf and if required toString. But I not able to get my head around how its actually working in the code given below […
Akash Singh
  • 387
  • 3
  • 7
0
votes
0 answers

what happens to leading zeroes during type coercion in array indexes?

say for an example, let years = [1950, 1960, 1970, 1980, 1990, 2000, 2010]; console.log(years['2'] === years['02']); // is false and console.log(years[2] === years[02]); // is true like i referred "2 is coerced into a string by the JavaScript…
Santa
  • 369
  • 2
  • 8
0
votes
1 answer

Is there a way to disable comparison any vs other type?

Typescript has pretty cool error message on checking different types: let strange_boolean = true; let strange_string: string = "1"; console.log(strange_boolean == strange_string); error: TS2367 [ERROR]: This condition will always return 'false'…
Denis Kotov
  • 735
  • 7
  • 22
0
votes
1 answer

Type coercion in AS3 Flash

I am making a simple jumper game in AS3 in Flash, and at this moment everything works but I get this note: Error #1034: Type Coercion failed: cannot convert 2 to flash.display.Scene. at scratch_theGame_kat_fla::MainTimeline/startkeyPressed() I…
Kat
  • 33
  • 6
0
votes
1 answer

Literal Int to Double coercion in arithmetic expressions

It appears that Swift applies floating point contagion (as it is called in other languages) to literal Int operands in an expression containing a Double variable before evaluating the expression. Is there an explicit statement about that somewhere?…
Robert Dodier
  • 14,751
  • 2
  • 25
  • 42
0
votes
2 answers

JavaScript filter function filtering out zeros in array

Please see code below. it runs but it does not return the correct value. For an input: [0,4,0] the output should be [0,0,0]
0
votes
1 answer

Why use '!!' before an expression?

I'm on VSCode with ESLint installed and I tried to type that expression to get a "true" or "false" output const result = goodGuys.includes(guy) ? but it didn't worked and with a quick fix (from ESLint that I don't understand, so here I am lol) of…
user11682335
0
votes
0 answers

Why Date object @@toPrimitive default value is toString?

Other object @@toPrimitive(default) first look object.valueOff() and if valueOff return object itself look object.toString method. But in date object this is different. This is interesting for me is this have reason?
Murad Sofiyev
  • 730
  • 5
  • 21
0
votes
1 answer

parse_ini_file converts values of constants into strings, even when using INI_SCANNER_TYPED

INI_SCANNER_TYPED, used as the 3rd argument to parse_ini_file(), should conserve boolean, null and integer values "when possible", according to the documentation However, when constants are parsed, the values are converted to strings. The…
Mark Fisher
  • 711
  • 8
  • 26
0
votes
0 answers

How to print data from a row in R

I want to print out all of the values in a row that correspond to a string in the first column. My df is as follows: head(df) Genes WT250 WT50 WT10 WT250-50 WT50-10 WT250.50 WT50.10 WT250.50.5p WT250.50.5n WT50.10.5p 1…
Lizzi Duncan
  • 63
  • 1
  • 5
0
votes
0 answers

How does explicit coercion works in R?

I am trying to understand the COERCION in R programming language. When it comes to explicit coercion, it states that we can convert from one class of vectors(considering general object) to another class. Consider following, > x <- c(1L, 0L, 3L) >…
Anil
  • 1,426
  • 3
  • 28
  • 55