Learning numbers and methods related to numbers in JavaScript
4 minute(s) read | Published on: Jun 04, 2021 Updated on: Dec 14, 2021 |
As you know, there are different programming languages that of them can help programmers a lot, these languages are being used to enter different variables, each of which can have values, and it is important to learn the basics of importing each one through different codes, as well as checking whether the imported codes are executed correctly or not.
One of the most popular languages for various programmers is JavaScript, through which you can enter numbers and various texts. In this article, we will look at numbers and methods related to them, but before that, it is necessary to give you a brief explanation about JS.
What is JS?
It stands for JavaScript. It is one of the programming languages through which complex features can be implemented in the simplest possible way on the web and has the ability to convert a web page from static to dynamic, which if you want to get more about it, you can visit our website to read the related article.
JavaScript Numbers:
In this language, numbers can be written in decimal or normal format, now note the following example where both types of numbers are given.
Exponent:
To write numbers that are extra small or extra-large, you can use the exponent, which we will give an example in the following.
Important note:
The important thing about numbers in this language is that JavaScript numbers are always stored as double-precision floating-point numbers, following the international IEEE 754 standard.
Adding Numbers and Strings:
In this section, you can see different cases. For example, two numbers may have been added to each other or two strings. Still, in general, the point that needs to be mentioned is that if you enter the code numerically in several lines, they will be added together, but if you do the same thing as a string, the strings will only join together.
If you add two numbers, then it is clear that the result you will see is a number at the end. To understand it better, pay attention to the following example.
The next thing that is likely to be done is to enter two strings, in which case the result will be clear, and in the end, what you will see as the result will be the merging of the two strings, for this case, there is an example below and the result of which is 4060.
Now imagine adding a number and a string, in which case the result will be as a string, and the two lines will join together. The result of the example which is mentioned below is also 4060.
* / - operations:
In these three operations, JS tries to provide numerical results because numerical data is added as a string. To understand it better, an example is given for each operation.
- In connection with the division operation:
- In connection with the * operation:
- In connection with the - operation:
Some important reserved words in JavaScript:
Not a Number:
NaN, which stands for Not a Number, is a reserved word in JavaScript displayed when you enter a code through which you want to enter a non-numeric string into the calculation.
Infinity:
Infinity (or -Infinity) is displayed when the result is outside the largest number defined in this language.
JavaScript Number Methods:
Number Methods help you work with numbers better, and we will discuss some available methods.
- toString () Method:
The toString () method returns a number as a string. We suggest you pay attention to the following example to understand it better.
- toExponential () Method:
To write a number using exponential notation, you can use this method, which shows the string as a number with its help.
- toFixed () Method:
In this method, you can specify the number of decimal digits when there is a decimal number.
- toPrecision () Method:
With the help of this method, the length of a number can be determined.
- valueOf () Method:
In this method, a number returns as a number.
Last word:
In general, there are several methods related to numbers, some of which we have described in this article so that you can use them while using numbers. We hope that the information provided to you in this regard can help you learn JavaScript to some extent.
Click to analyze your wesbite SEO