29.07.2016 Views

front-end-developer_1_

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Front-End-Developer - Level 1<br />

06.Methods<br />

Embedded Video: https://www.youtube.com/watch?v=fBZvoT9WfMs?rel=0<br />

Now that you've got the basics of numbers down, let's learn how to manipulate them a bit.<br />

First, let's change a number into exponential notation.<br />

If you're not familiar with exponential notation, here's how it works. Take the number 78.5. In<br />

exponential notation, we write it 7.85 10. Or take 356.97; in exponential notation, that's<br />

3.5697 102. You can also write it as 3.5697e+2.<br />

Exponential notation makes it easy to write very large or very small numbers. For example,<br />

1,000,000,000 becomes 1e+9, and 0.00000002 becomes 2e-8.<br />

We can use JavaScript to easily change numbers into exponential notation:<br />

>48432.78.toExponential();<br />

"4.843278e+4"<br />

toExponential() is called a method. You can think of a method as a message that you s<strong>end</strong><br />

to a number, and the result that JavaScript gives you as the number's response to that<br />

message.<br />

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

>4.587e2.toFixed();<br />

"459"<br />

The toFixed() method will round to the nearest whole number. Here's how we can tell it<br />

how many decimal places to use:<br />

46.1.toFixed(2);<br />

46.10<br />

The 2 in the parentheses is an argument to the toFixed() method. Arguments provide a<br />

bit more information to methods to help them know what they're supposed to do. In this<br />

case, the argument is optional. When a method doesn't take an argument, or when the<br />

argument is optional and you aren't using it, you still need the parentheses on the <strong>end</strong> - so<br />

you have write 1.05e3.toFixed() , not 1.05e3.toFixed .<br />

Methods<br />

104

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!