Javascript quirks Memes

Posts tagged with Javascript quirks

JavaScript Is Weird

JavaScript Is Weird
So you're telling me that adding the string 'b' to 'a' twice, then adding 'a' twice more, and calling toLowerCase() somehow produces "banana"? Yeah, that tracks. JavaScript's type coercion is basically that friend who always "helps" by making things infinitely more confusing. Here's what's happening: 'b' + 'a' gives you "ba". Then + + converts the next 'a' to NaN (because unary plus on a string that's not a number = NaN). "ba" + NaN = "baNaN". Add another 'a' and you get "baNaNa". Call toLowerCase() and boom—"banana". It's like JavaScript is gaslighting you into thinking this makes sense. The real question is: who discovered this, and what were they doing at 3 AM to stumble upon it?

Compute Fibonacci In JavaScript

Compute Fibonacci In JavaScript
JavaScript's type coercion strikes again. Someone tried to compute the Fibonacci sequence but forgot that adding strings together doesn't do math—it does concatenation. So instead of getting 1, 1, 2, 3, 5, 8, 13, you get "1", "11", "111", "1111"... just progressively longer strings of ones. It's like watching someone try to do arithmetic with duct tape. The best part? The code probably ran without errors. JavaScript just silently nodded and said "yeah, this seems fine."

This Is Javascript

This Is Javascript
Someone enthusiastically introduces their favorite language, and JavaScript immediately demonstrates why it's both loved and mocked in equal measure. The plus operator does string concatenation ("11" + 1 = "111"), while the minus operator coerces to numbers ("11" - 1 = 10). Totally logical and not confusing at all. JavaScript's type coercion is like that friend who tries to be helpful but just makes everything worse. The language sees a plus sign and thinks "maybe they want strings?" but sees a minus sign and goes "definitely numbers here." It's the programming equivalent of a chaotic neutral alignment.

Basically Free Money

Basically Free Money
Oh, the absolute JOY of floating-point arithmetic in JavaScript! Nothing screams "professional financial software" quite like receiving 3 dimes and somehow ending up with $0.30000000000000004 because JavaScript's Number type decided to have an existential crisis about decimal representation. It's like asking for exact change and getting handed the mathematical equivalent of "close enough, right?" Binary floating-point numbers can't represent 0.1 precisely, so when you do basic math, you get these delightful microscopic errors that haunt your financial calculations. But hey, that extra 4 quadrillionth of a cent? That's YOUR bonus for trusting JavaScript with money calculations. Stonks! 📈

No Doubt Javascript

No Doubt Javascript
JavaScript's type coercion strikes again with its legendary logic. Using the strict equality operator (===), octal 017 doesn't equal decimal 17 because JavaScript interprets that leading zero as "hey, this is octal!" (which is 15 in decimal). But 018? That's not a valid octal number, so JS just shrugs and treats it as decimal 18. Then comes the double equals (==) where JavaScript becomes the chaos agent we all know and love. It converts the string to a number and suddenly everything makes sense... in the most JavaScript way possible. The language where "wat" is a valid reaction and type coercion is both your best friend and worst enemy. This is why we have trust issues.

JS Is A Very Respectable Language

JS Is A Very Respectable Language
JavaScript really said "consistency is for COWARDS" and honestly? It committed to the bit. 💀 So you've got an array [1, 2, 3] and you're like "hey what's at index -2?" JavaScript casually returns undefined because negative indices don't exist in JS arrays... EXCEPT when you use .at(-2) which is specifically designed to handle negative indices and suddenly it's like "oh you want the second element from the end? Here's your 2, bestie!" Then you assign foo[-2] = 4 which JavaScript happily accepts because arrays are objects and you just created a STRING property called "-2" on that array object. So now foo[-2] returns 4 from the object property while foo.at(-2) STILL returns 2 from the actual array position. Same syntax, completely different universes. Very respectable. Very normal. Nothing to see here. 🎪

Stop Doing NaNs

Stop Doing NaNs
Ah, the eternal JavaScript nightmare: NaN (Not a Number) - which ironically is a number type that doesn't equal itself. Because that makes perfect sense! The IEEE 754 floating-point standard really outdid itself here. "Let's create a special value that represents calculation errors but make it behave in the most counterintuitive ways possible!" My favorite part is JavaScript trying to be helpful: "You want to convert 'hello' to a number? Sure thing! Here's a NaN for your trouble. No errors thrown, just silent mathematical chaos." And then we wonder why our date calculations suddenly think it's the year NaN. The hex(983061) at the bottom is the cherry on top - it's 0xF00D61, or "FOOD A1". Even the hexadecimal is trolling us.

The Magic Number Of Zeroes

The Magic Number Of Zeroes
JavaScript's parseInt() function is like that one coworker who ignores all your emails until you add exactly seven zeroes after the decimal point. The function stubbornly returns 0 for every decimal value, until suddenly—at 0.0000005—it decides "Oh, I see a 5 now!" and returns 5. It's like watching someone squint harder and harder at tiny text until they finally give up and just read whatever letter they think they see. The floating point precision gods have spoken, and they've chosen chaos.

The Sort Of Surprise Every JavaScript Developer Deserves

The Sort Of Surprise Every JavaScript Developer Deserves
Innocent newbie: "I'll just use array.sort() to sort these numbers!" JavaScript: *sorts lexicographically* "Did I stutter?" Nothing says "welcome to JavaScript" quite like discovering your numbers are being sorted as strings. That moment when you realize you need array.sort((a,b) => a-b) and question all your life choices that led you to web development. It's basically JavaScript's hazing ritual - "Oh, you thought programming would make sense? That's adorable."