Don't understand math object or constructors?

https://www.codecademy.com/paths/web-development/tracks/getting-started-with-javascript/modules/learn-javascript-introduction/projects/kelvin-weather-javascript

I was doing this project and I read that the ‘math’ object is not a constructor.
I find the theory behind this complex so could someone please explain in simple english what this means and why maths is the exception.

I don’t understand how the following is possible:
fahrenheit = Math.floor(fahrenheit)
Does this mean the fahrenheit variable is being reassigned? But to what because Math.floor is an object.
I guess I am not sure what Math means.
Thanks!

Hello @board1774168228! This rounds down the variable fahrenheit, and stores that to fahrenheit. The Math object is a static object, meaning that all methods within it are called on the class itself (Math), as opposed to an instance of that class. Take Math.floor(). The method .floor() rounds a number down, however, you call this method on the Math object itself, rather than calling it on an instance of the Math class.
In other words, you provide a number as a parameter for the method, instead of directly calling the method on the integer.

let a = Math.floor(3.5);
//when we call the method floor, we are calling it on the class itself
//(Math), and provide a parameter 3.5.

To provide contrast, take the .push() method (which pushed a value onto a list):

//an example of .push():
let a = [1, 2, 3, 4, 5];//list of integers
a.push(6);//pushes 6 onto the end of the list a
//a is now [1, 2, 3, 4, 5, 6]

The .push() method acts on an instance of the list class (in this case, the variable a).

This describes Math more, and this describes what static class are more.

I hope this helps!

1 Like

Thank you so much this makes a lot of sense!

I just had one last question. Which object would that variable a belong to? Because you called .push() method to the list class/a variable and not the object itself, I know it must be non-static object, but what exactly is the object?

Thanks

Hello @board1774168228. The object that the variable a belongs to is the list class. Classes and objects are very similar-a class is just the definition, for lack of a better word, of a type of object.

Yes, you are right, it would be a non-static object, and the object is an array. You should cover this in a later module in JS (I presume you came from Java or a heavily OOP orientated language, but the later lesson cover classes/objects within JS).
Happy coding!

1 Like

Thank you so much Aidan!

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.