02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoPrevious Video

Numbers

<p>Numbers are an essential part of any language including JavaScript. </p> <p>We have already covered that JavaScript has three data types: Strings, Numbers, and Booleans. It only has one variable to store all three data types; it&rsquo;s up to you to look at this as a pro or a con. It&rsquo;s a good idea to make peace with it and treat it as a pro; you know, the glass half-full approach.</p> <p>Moving back to numbers, we can kill two birds with one stone. First let&rsquo;s see what PROMPT is in JavaScript. &quot;User input&quot; is when you ask the user for data. How exactly will the data come to us so we can access it in our code and manipulate it? &quot;prompt&quot; is the answer for now(in a real live application if it was 1987 but for now would be the correct answer as well). prompt() is a function which permits us to get data from the user: watch the video carefully and you will see how it provides this interface.</p> <p><b>prompt()</b> accepts two string parameters. The first one is the message which will be displayed on the pop-up box like &ldquo;How Old Are You?&rdquo; while the second argument is what should be displayed in the text box where the user will input the data. This argument is usually left blank unless you want users to have a certain default string so they can just click OK.</p> <p>What will &quot;prompt&quot; return to us? Whatever the user enters in the prompt box will be returned as a string. Remember &quot;prompt&quot; only returns a string, even if the user enters 23. &quot;23&quot; will be stored in a a string and cannot be used as a number. Here it is in action: </p> <p><i>var userAge = prompt(&ldquo;Enter your age&rdquo;, &ldquo; &rdquo;);</i></p> <p> <h3>In depth look</h3> <p> Let&rsquo;s look at this string returning in detail. We have a variable var num1 which has 10 stored in it: we will ask the user for a number which we can add to variable num1. We use &quot;prompt&quot; and ask the user for the second number, a pop up box is displayed with the message &ldquo;Please Enter a Number&rdquo;: the user types 10, which we store in another variable called var num2. Now we display the result using &quot;alert&quot; like this:</p> <p><i>alert(num1 + num2); </i></p> <p>What do you think we will get?</p> <p>If you answered 20 -- BEEP. Your logic is right but the answer is wrong, JavaScript does not know that num1 and num2 are Numbers, remember? It assumes they are strings and simply concatenates num1 and num2. The alert will give us &ldquo;1010&rdquo; rather than 20.</p> <p>How do we solve this? JavaScript provides us with numerous functions. You just have to learn to choose the right function at the right time. In this case we want to add num1 and num2 -- we use the function <b>parseFloat();</b> it accepts a string argument and outputs a number. Simple as that.</p> <p>Now when we call &quot;alert&quot; like this:</p> <p> <i>alert(parsetFloat(num1) + parseFloat(num2)); </i> </p> <p>We will get the desired result of 20, because <b>parseFloat();</b> converts our string inputs into what JavaScript now recognizes as numbers: therefore it adds them instead of concatenating the strings.</p> <p><b>ParseFloat();</b> can also convert to decimal values. If we want an integer there is <b>parseInt();</b> which returns only integer values. We can additionally perform all the basic math functions on variables such as num1/num2, num1*num2, num1-num2. Play around with this and you&#39;ll quickly learn it.</p> <p>It&rsquo;s a good idea to watch this video more than once as it will help you understand how JavaScript deals with numbers and converts them from strings.</p>

Intro

It's never been easier to start learning programming in JavaScript. We live in an HTML5 world: what would be better than learning how to code with JavaScript and HTML5?

03:59

The Script Tag

You must know about script tags in order to code in JavaScript, so let’s jump into it and learn with style!

04:26

OOP and DOM

Two basics which are very important in programming and provide discipline which helps you learn and stay organized

02:45

What are Variables?

Variables are the building blocks of any language, so it's best to be very familiar with them and their usage.

01:37

Strings

Strings are an important part of programming; they can be manipulated, passed as parameters and a lot more.

10:39

Creating Javascript Comments

Comments are a great asset of programming. They will help make the code more understandable and manageable

00:52

Numbers

Math is everywhere; numbers are everywhere. JavaScript lets us use them and apply math on them, so let’s have fun and learn about Numbers in JavaScript.

09:29

Boolean

Booleans help us make important decisions in programming by proving yes or no information.

09:10