JavaScript's Hidden Pitfall: Leading Zero in Numbers

JavaScript looks simple… until it doesn’t. Ever wondered why this code behaves differently in old vs modern JavaScript? if (08 == 8) { console.log("equal"); } else { console.log("unequal"); } 🥲Old JavaScript (pre-ES5) : Numbers starting with 0 were treated as octal (base-8). 08 → invalid octal Often interpreted as 0 since 8 is invalid octal number. Result: 0 == 8 // false So, Output: unequal 😄 Modern JavaScript (ES5+) Leading zero no longer means octal. 08 is treated as decimal 8 Result: 8 == 8 // true So, Output: equal 💡 The real lesson JavaScript evolved to remove confusing behavior Backward compatibility can hide dangerous bugs Always write clear, explicit code 👇Now use this 8 // decimal 0o10 // explicit octal (equals 8) Small details. Big bugs. That’s JavaScript for you. 😄 (Note - now you can't flex in front of non tech people that 018!=18 nor you can try this with Modern javascript 😑) #JavaScript #WebDevelopment #Programming #CodingTips #Developers #Tech #SoftwareEngineering LinkedIn -- Pralhad Saw

  • No alternative text description for this image

Lion be like - Jungle mein d Dhahadne se ghar nhi chlta mitra...

Like
Reply

To view or add a comment, sign in

Explore content categories