LetMyPeopleCode.com

A blog about software, schmaltz, and monkey-patches for the soul

Menu
  • About Greg Bulmash
  • Greg Talks Tech
  • Privacy Policy
Menu

JavaScript v. Python: Type Coercion

Posted on December 29, 2022December 29, 2022 by Greg Bulmash

While I’ve got the time, I’m refreshing on the basics of Python before I delve into becoming better at it, and I’ve run into some things I either missed the first time(s) round or that stood out to me as different than JavaScript. The best one this morning was a simple reminder of JavaScript’s type coercion.

Both JavaScript and Python are dynamically typed, meaning you don’t have to declare a type to assign a value to a variable and you can change that variable’s type mid-stream by assigning a different type of value to it.

What is type coercion in JavaScript?

It simply means if you try to add or concatenate two different variable types, JavaScript will usually coerce one to be the same type as the other to make this work. Let’s give a simple example…

let a = 2;
let b = '3';
let c = a + b; 
console.log(a, c)

The output is going to be: 2 '23'

This means that the value of a, which is an integer, was coerced into a string, then 2 and 3 were concatenated like two strings being added together rather than two integers being added together.

The value of a remains an integer, but it was used as a string for the calculation. This will hold true even if you declare a with const instead of let because the value that’s coerced is a temporary copy, not the variable itself.

That said, you could redefine a as a string containing “boo” in either language at some point before adding a and b together. Neither JavaScript nor Python would throw an error or warn you that the variable’s type had changed. You’d just get 'boo' 'boo3' as the result.

Meanwhile, in Python, adding 2 to ‘3’ would throw an error before you got to logging the values. But if you had added the line changing 2 to ‘boo’ before the line adding the values, you’d get the same logged values as JavaScript.

Type coercion is dangerous

One of the things that makes dynamically typed languages dangerous is the fact that you can’t always rely on a to be an integer. The three levels of equal-sign usage in JavaScript are both magnificent and horrifying, depending on your point of view.

a = b; // assigns the value of b to a
a == b; // will use type coercion to compare the two if possible
a === b; // uses strict equality, both must be the same type and value

Those double equals can get tricky.

'1' == 1 // true
'1' === 1 //false
0 == false // true
0 === false // false

An integer can be coerced into a string or a boolean when you’re using two equals. That’s why best practices for JavaScript suggest generally avoiding it and always using three equals.

But Python doesn’t coerce types

If you Google “does python coerce types,” the top answers will all say no. What does that mean?

'1' == 1 // False, because no coercion
'1' + 1 // error because you can't add str and int types with the + operator

Python doesn’t have a triple equals out of the box because it doesn’t need it. As it doesn’t coerce types, it doesn’t need a special comparison mode that prevents coercion.

While '1' == 1 is a true statement in JavaScript because of type coercion, it’s a False statement in Python. Also worth noting that I use ‘False’ instead of ‘false’ for Python because it uses a capital letter at the beginning of its boolean values, which threw me the first time I encountered that.

I know, not earthshaking, but something that stood out to me enough to be worth writing about.

Related

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • Job Hunt Diary 2023: Are you Interested in… and it’s closed
  • Comcast XFinity home internet vs T-Mobile home internet
  • Job Hunt Diary 2023: Canonical doubles down on questions that could cause bias
  • What does a Developer Advocate do?
  • Job Hunt Diary 2023: You’ve clearly mistaken me for someone else

Archives

  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • September 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • November 2021
  • May 2021
  • April 2020
  • March 2020

Categories

  • Apps
  • Computing
  • Frameworks and Libraries
  • Games
  • Hacks
  • Hardware
  • JavaScript
  • Mac
  • Mobile
  • Other Art Projects
  • Productivity
  • Programming
  • Projects
  • Python
  • Society & Culture
  • Teaching Code
  • Tech Career
  • Uncategorized
  • WebTech
  • Writing

Tag Cloud

Adobe browser hunt code sample comcast commute CX Developer Relations dev rel evernote game demo google drive hardware review health home office home studio internet interview questions j job hunt job hunt diary Mac magento meta-coding OneNote productivity review speedtest t-mobile Windows xfinity

©2023 LetMyPeopleCode.com | Theme by SuperbThemes