Lesson Weekend

Now that you've learned the basics of numbers and strings, let's learn how to manipulate them a bit. First, let's change a number into exponential notation.

Exponential notation is essentially just a way of shortening really long numbers, or really small numbers by removing their zeros. If you're not familiar with exponential notation, here's how it works. Take the number 78.5. In exponential notation, we write it 7.85 * 10. Or take 356.97; in exponential notation, that's 3.5697 * 102. You can also write it as 3.5697e+2.

Exponential notation makes it easy to write very large or very small numbers. For example, 1,000,000,000 becomes 1e+9, and 0.00000002 becomes 2e-8.

We can use JavaScript to easily change numbers into exponential notation:

``````> 48432.78.toExponential();
"4.843278e+4"
``````

`toExponential()` is called a method. You can think of a method as an action that a number can perform or have taken on it. There is an important difference between a method and a function, but we are not ready to explore this quite yet. For now, just know that if you did not write the function, it is 99% likely that the function is actually a method. This method is called on a number to change it to exponential form. JavaScript gives a response to this method's call. The response is "4.843278e+4" which is known as the method's return value. In the example above, we call the toExponential() method on the number 48432.78, and the return value is the string "4.843278e+4".

You can also go in the other direction, and convert out of exponential notation:

``````> 4.587e2.toFixed();
"459"
``````

(Note here that `4.587e2` means the same thing as `4.587e+2`). The `toFixed()` method will round to the nearest whole number. Here's how we can tell it how many decimal places to use:

``````> 46.1.toFixed(2);
"46.10"
``````

The `2` in the parentheses is an argument to the `toFixed()` method. Arguments provide a bit more information to methods to help them know what they're supposed to do. In this case, the argument is optional. When a method doesn't take an argument, or when the argument is optional and you aren't using it, you still need the parentheses on the end - so you have to write `1.05e3.toFixed()`, not `1.05e3.toFixed`.

Here's another method that takes an argument:

``````> 8.12345.toPrecision(4);
"8.123"
``````

You can also call a method on a variable, since the variable is just standing in for the number it represents:

``````> const myNumber = 8.12345;
undefined
> myNumber.toPrecision(4);
"8.123"
``````

If you already have experience in Object-Oriented Programming, it might make sense to you that a method is a function that is defined in a class. Purely object-oriented languages such as Java don't, as a rule, have functions except in special circumstances.

## Terminology

• Method: A method is an action run on a piece of data; you can think of it as a message you send to a piece of data, and the result is the response.

• Return value: The return value is the method's response.

• Argument: Some methods take one or more arguments that provide the method with additional information to help it perform its action.

## Examples

The `toExponential()` method is an action that can be run on a number. It returns a string representing that number in exponential notation:

``````> 48432.78.toExponential();
"4.843278e+4"
``````

The `toFixed()` method is an action that can be run on a number. It returns a string representing that number without exponents:

``````> 4.587e2.toFixed();
"459"
``````

Some methods take optional or required arguments in the parentheses after a method:

``````> 46.1.toFixed(2);
"46.10"
> 8.12345.toPrecision(4);
"8.123"
``````

Remember to put parentheses after every method, even if you are not putting an argument in them.

You can call methods on variables:

``````const myNumber = 8.12345;
myNumber.toPrecision(4);
``````

Lesson 11 of 61
Last updated October 12, 2020