Or better yet just look at some code examples, and learn by doing.
A number can be a value that is returned by a function call. When creating a function there is using the return keyword in the body of the function, and making it so that it will return a number value. There are also a number of built in static functions such as the Math.pow method.
Numbers are one of many values that can be passed to functions as arguments. So often functions are made that will expect a number as an argument, or at least will only return a desired result if a number is passed. Inside the body of a function if I declare a variable and assigned a number to it, or any value for that matter, it will become local to that function even if I use var as the way to do so. If I use let and const it can event be local to a block of code inside the function, but variable scope is maybe a bit off topic here.
As you can see from the basic examples numbers can be the result of expressions, what is returned by a method, and can also be used as arguments for a function. In the body of a function numbers can be declared with the var let or const keywords and when doing so will have function or even block level variable scope inside the body of that function when used with those keywords. There are also a number of number class prototype methods such as toFIxed that can be called off of any instance of a number.
When I want to find out if a variable or property key value that I am dealing with is a number or not one way to go about doing so is to use the typeof operator. To use this operator I just type typeof and then the value that I want to check, or an expression that will evaluate to a type.
In this example I am using the array filter method to filter out all elements except elements that are of the type called number. This works by calling the filter method off of the source array, and then passing a function that will be called for each element in the array. If the return value of the function is true, then the element will be in the new array that will be returned, else it will not. So I am just using the typeof operator with the current element, and seeing if that compares to the ‘number’ string using the identity operator.
When making an equal comparison between n and n + 1 the returned value should be false. However When I go beyond the Max Safe integer this is no longer the case. The reason why has to do with the limitations of IEE_754. For many projects I am not working with numbers this large, but when I do I will want to use a library that represents numbers as strings, or make use of the new bigInt standard to help get around this.
One more thing about the Number.toString prototype method, there is an option to give a base as the first argument to this method. So if you are looking for a quick way to get a string representation of a number in binary for example the number to can be given to the toSting method sense binary is a base two system.
For the most part using the Number function is the standard way of converting something that is not a number to a number, I say that mostly because of the valueOf method of objects. Many built in constructors such as Date have valueOf methods, and as you can see in the example above it can be included in my own custom objects as well.
One trick is to multiply the string by 1, if the string can successfully be converted to a number it will result in a number with the corresponding value, otherwise it will result in NaN. There is also parseInit,parseFloat and the Number method as well that can work.
The parseInt and parseFloat methods are yet another way to go about creating numbers from string values.
So I have covered creating a String from a Number value, and creating a Number value from a string, but what about Objects. Well then it comes to this there is the valueOf method of an object. This method can be a so called own property of an object, or it can be part of the prototype object of a class of objects.
When creating an expression of one or more operations a Number can often be the result of that expression. In some cases however it can some times evaluate to something else such as a string depending on the data type of the value. For example if an operator such as the multiplication operator is used with a Number and a string the string will be converted to a number, and the result will be a number. However if addition is used with two strings then the result will be string concatenation, and not addition.