Numbers in JavaScript introduction
Numbers in JavaScript are as straightforward as they sound; you don’t need any special syntax for numbers, you write them straight into JavaScript.
In JavaScript, numbers can be whole numbers (called integers). For example:
Or numbers with decimal points to represent fractions of a whole number like:
Numbers like these with decimal points are also called "floating point numbers." JavaScript even lets you use scientific notation to represent really large or really small numbers:
Storing Numbers in Variables
Just as with string values, you can put a number in a variable, or "assign" a number to a variable using the equals sign (or assignment operator):
Unlike strings, you don't put quote marks around a number. If you do, then you have a string, not a number. This is one confusing part about working with strings and numbers. For example, consider the following code:
The value stored in the variable aString
is not the number 10. It's a string made up of the character 1
followed by 0
– this can lead to strange and confusing behavior like making some math operations not work as expected. We'll look at how to convert a string containing a number into an actual number in a later video in this stage, but keep this in mind: a number inside quotes isn't really a number.
Comments
Post a Comment