91 for loop example in JS - JavaScript Tutorial 2018

preview_player
Показать описание
The Full #Javascript #tutorial for #beginners :

Learn HTML, CSS and JavaScript

JavaScript Tutorial 2018

91 for loop example in Javascript - JavaScript for Beginners.

This is a Javascript tutorial from scratch for any beginner. Use the above link to watch the full JS video tutorial..

Hi and welcome back.

So before we diverge in to 10, x+10, let’s do an example whereby we convert this into a ‘for’ loop.

And the way we are going to do it, we can declare this up here, we can declare it down here.

So I want to put this down here so that you can see the difference.

So for, let me take this, Control+X and let me just put it here and I will comment all of this.

So for, then let me copy this.

var x=1.

You can even use let.

If you remember let, you can use var or let.

And then, the next part we have here is the condition.

Our condition is x less or equal to 5.

So this is not a comma, this is a statement.

It’s very important for you to understand that this is a statement.

So you terminate it using a semi-colon to terminate the statement.

And then, the condition is also a statement.

And then after that, you’ll have your increment.

Increment, and then you can alert the value of ‘x’ and this will print for you ‘x’ five times.

So if I run, it starts from 1, 2, 3, 4, 5, and then it runs out.

So I also said that we wanted to see that we don’t necessarily have to use x++.

So let’s say we want to print 10, 20, 30, 40, 50.

What we are going to do is first of all, let’s make this up to 50.

So x less or equal to 50.

And then we are going to say x+10.

And if you do it just like this, you’ll get an error.

You have to say ‘x=’, you have to give ‘x’ the value of x+10, like that in full.

And then, we have to change this to zero because if this is 1, it will print 11.

It will print 1, 11, 21, 31.

So let’s make this zero.

So if we run this, it should print 0, 10, 20, 30, 40, 50, and then it will end there.

So you can see that it doesn’t have to be x++.

So another way of writing this x=x+10, you can also write it as x+=10.

So this is a shorter way of writing x=x+10.

So if I do this and I run, it will give us 0, 10, 20, 30, 40, 50.

So this is also the same as x++ if we remove that and x+=1, this is the same as x=x+1 which is also equals to x++.

If we remove that and we run, you’ll see that is true.

So that’s it for this video.

If you have any questions, let me know.

And for your assignment, you can print 0, 30, 60, 90, 120, all the way up to 240.

Try that and let me know if you have any problems.

So I’ll see you in the next video.

GET SOCIAL WITH ME:
Рекомендации по теме
visit shbcf.ru