Skip to content
Advertisement

why can’t I override Math.PI but can override Math.floor in JavaScript?

I tried assigning Math.PI to 1000000, but when trying to use the value, I still got the default non-overridden value, while I was able to override Math.floor with function(){console.log('floor')} and calling it called the overridden function. I was wondering why that is and how I’d be able to override Math.PI. I tried this on both node v14 and Chrome v83.

Advertisement

Answer

You can check Math.PI descriptor using Object.getOwnPropertyDescriptor(), and you’ll see that this property in not writable. That is why when you try to reassign its value, it is not being updated

console.log(Object.getOwnPropertyDescriptor(Math, 'PI'))
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement