Lesson 05
Doing Basic Math in JavaScript
Summary
# JavaScript Course: Exploring Numbers and Operations
Welcome back to nbktech world! In this video, we continue our JavaScript course focusing on **numbers** and basic **mathematical operations**.
## Key Points Covered
### Understanding Numbers in JavaScript
- JavaScript recognizes only one type for numbers: `number`, which includes both **integers** and **floating-point** numbers.
- No distinction exists between integer and decimal numbers in JavaScript.
### Basic Math Operations
- We explored four basic operations:
- Addition: `47 + 13` gives `60`
- Subtraction: `77 - 8` results in `69`
- Multiplication: `3 * 9` equals `27`
- Division: `12.3 / 3` initially gives `4.1`, but may show an extended floating-point result due to precision issues.
### Special Cases
- **Division by Zero:**
- `7 / 0` results in **Infinity**.
- `0 / 0` results in **NaN (Not a Number)**, indicating an undefined mathematical result.
### Order of Operations
- JavaScript follows standard mathematical precedence:
- For example, in `2 + 3 * 3`, multiplication is evaluated first, resulting in `11`.
- To change the order of operations, use parentheses:
- `(2 + 3) * 3` evaluates to `15`.
### Conclusion
- Remember, the console can be a useful tool for quick calculations and exploring JavaScript quirks.
- Be aware of peculiarities such as *Infinity* from division by zero and *NaN* from undefined computations.
Thank you for joining this session! Please **like and subscribe** for more videos. Until next time, happy coding!
Video Transcript
Welcome back to nbktech world.
Let's go on with our JavaScript course.
Like I've already talked about variables, we learned how to define a variable using
the keyword cost.
Then at the end we learned about a new type of value in JavaScript which is the number.
So we now know there are strings and there are numbers.
This video will be all about numbers.
We're going to learn some operations on the numbers.
We're going to do some math.
Alright, so just a disclaimer, we're going to do some basic math operations, addition,
subtraction, multiplication and division.
So let's get going.
First I want to switch to new browser so we can have some variation.
And I'm going to use, let's see, let's use Safari.
Here I'm Safari.
I have my browser console open.
Remember you can click develop and then show JavaScript console.
So let me increase the size here so you can see better.
So here at the bottom, alright, let's do some math.
So in JavaScript there are numbers.
These just appear as they are.
So 10, 23.4, so on.
Okay so in JavaScript there's no distinction in terms of type between an integer number
and a decimal number.
If you already know some programming, right, you know there's always what are called the
integers versus the floating point numbers.
So for JavaScript there's only one type and there's the type number and that supports
that have a decimal point or not.
That's it for that stuff.
You don't have to really get that right away.
So let's just do some operations here.
Let's try addition.
So you can just use the console to do some math for you.
So you can type a number plus another number.
So for example 47 plus 13 will give you 60.
So just use the plus operator.
The same thing.
77 minus 8 is going to be what?
69.
And that's right multiplication.
Three times nine.
27, right?
So that's the multiplication operator.
And then let's try 12.2 divided by, let's try 12.3 divided by 3.
Okay, so that's a, wow.
There's a big, big, big, big number.
So if you think about this in terms of real life math you can do 12.3 divided by 3 should
be 4.1.
But our JavaScript console gave 4.10000 out to 5.
So it's not exactly correct.
And that's because of floating point numbers.
The computers cannot exactly tell you the exact value.
So this is like a, there's a very slight error, precision error.
It can be accounted for if you're doing like real world stuff.
There's the concept of a big number type and all that kind of stuff.
I don't want to get you into that.
We're just beginners here.
So what you need to be aware of is whenever you have decimal numbers like the result is
a decimal it's not going to be 100% precise.
There's going to be a slight error there.
So be aware of that.
Okay.
Put that in the back of your mind.
And it might come up someday if you're doing some programming and really hardcore computations.
All right.
So be careful with decimal numbers.
So called floating point.
All right.
So we did addition, subtraction, multiplication and division.
Now what happens if you divide by zero?
Oh my God.
Let's try.
Seven divided by zero.
Oh my gosh.
What just happened?
Seven divided by zero gave us an infinity.
All right.
So that's apparently some value in JavaScript.
So watch out for division by zero.
You might get infinity.
Now what happens if I divide zero by zero?
Wow.
I got NAN.
What does that mean?
Well that just means not a number.
So it's kind of like a JavaScript thing where if you try to do some operation on, you know,
and the result is it couldn't really get a number result out of it.
So it says it's another number, NAN.
All right.
So watch out for that too in JavaScript.
You don't have to memorize this.
Just be aware these things might happen.
So you want to try experimenting in doing your journey with JavaScript.
There's going to be many weird things that they're going to pop up.
All right.
This is just some weird things in JavaScript, the way it came about.
And this is one of them, not a number.
All right.
So we learned division by zero by its gives of infinity and then zero by over zero, not
a number.
I think we're good in terms of operations.
I just want to end with the order operations, right?
Because say you want to do, we all know that like in math, if you have something like two
plus three times three, the first operation that's going to take place has to be what?
The multiplication because it has higher precedence, right?
The same is going on here.
So you're not going to get first the two plus three.
That's not going to happen.
It's going to obey the mathematical rules.
So you're going to have first is going to do three times three, which is nine.
And then it's going to add to two, got 11.
All right.
So be aware of that.
So it follows the basic mathematical principles of order operation.
Now if you really want to have two plus three occur first, then what do you have to do is
add parentheses, right?
So you can add a parentheses between these guys and it will first do this operation before
doing the multiplication.
So if you have parentheses in closing two plus three, first is going to do that.
So it's going to be five.
And then after that is going to be five times three, which is 15.
Okay.
So you can use the parentheses to change the order of operation to force the plus or the
minus or whatever operation to come first to take higher precedence.
Okay.
And I know that in math we have like, if you have parentheses like this, then you have
to put the square and then the braces outside to like to give a separate the kinds of precedences
about in programming here and in JavaScript to you only need for all of them, no matter
if it's square or the brace, you always use parentheses.
Okay.
So you can put two plus three.
And then you have, let's say, I don't know, times three, then nine.
You can put all these guys like this.
All right.
And whatever, you know, so it's always parentheses to make those guys like this has to take place
first instead of the multiplication.
And this has to take place first instead of whatever you put here.
I don't know what it is.
It could be anything.
Okay.
So I think we're good for this video.
We learned simple mathematical operations.
Your console is your friend.
If you want to do some mathematical calculations, it's there for you.
Just open it up.
If you have any browser, just open it up and you can do some calculations.
Learn about a few quirks about JavaScript, division by zero with an actual number over
zero, given infinity and then zero over zero, given NAN, which is called not a number.
Okay.
So thank you so much for watching and be K-Tac World E.
And please like and subscribe.
And until the next time.
Bye.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: