Why JavaScript allows adding functions to Math, but Java doesn't

Recently, I was asked a JavaScript question in an interview that honestly stumped me at first 😅 — it really tested my core JS understanding. 💡 Question: “Why does JavaScript allow you to add functions to the Math library, but Java does not?” At that moment, I couldn’t answer it. Later, when I looked it up, I found the explanation really interesting 👇 In JavaScript, Math is just a normal object — dynamic and extendable. You can add new methods like this: Math.square = function(x) {  return x * x; }; console.log(Math.square(5)); // ✅ 25 But in Java, Math is a final class with static methods — you can’t inherit or modify it. This keeps the system stable and predictable, which aligns with Java’s strong type and compile-time safety philosophy. I found this question such a great reminder of how JavaScript’s flexibility and Java’s rigidity come from totally different design goals. #JavaScript #Java #InterviewQuestions #LearningEveryday #Developers #Programming

To view or add a comment, sign in

Explore content categories