top of page

Introduction:

Next, I'd like to talk about a common gotcha that a lot of beginners the javascript kind of struggle with. Even if you are coming from a different programming language and it has to do with if you're trying to essentially capture the value and run some kind of logic or a callback on that value. So it does show you what I mean by that best digital marketing company in Noida. First of all just to explain what we're going to do we're going to basically use set timeout to print out numbers after a given amount of time and said Time is a built-in function and javascript that works like this. We have used it a little bit here and there.

 

Basically, give it a function like so and then you give it the amount of time you want the function to take to execute. So if we want our functions to take one secondary digital marketing company in Noida So essentially after one second, the function will get triggered. So if we want to cancel log something after the second one second has passed for example.

How to apply? 

So if I run this is run we wait one second and then we see that it pops up in two consoles and that's pretty cool. Now, what if we wanted to throw this into a for loop kind of print Thota number after a second or print out all the numbers after a second. For example, while your first instinct might be to do something like this you're going to do for the best digital marketing company in Noida unamusing bar here and I'll explain why shortly. But basically, I'm going to var bar I equal zero is less than plus equals.

 

And then in our block let's just throw in a set time out here said time throw in a function like so any time we want it to take and our function we're going to console log out. I and my Linter is probably not happy because I have a function inside me. I don't make function within a loop. And anyway let's see what happens here. So after second, all the numbers will pop up and you might expect to see you know the numbers. Let's see what happens.

 

We'll run it one second past Calix you didn't quite catch the change in our code. Try that again. There we go. And you might be thinking oh jeez that's not what I expected and I can't tell you the number of times that I've been burned digital marketing company in Noida by this when I was just starting out. And you think oh no that's not what I wanted. I mean I have to rewrite this a different way. I don't know why this is happening but it just happened so I have to rewrite this a different way and that's fine. But I think we should take some time to understand what exactly is happening here why this is happening.

 

Factors:

And it has to do with the way that we're initializing are for you. So essentially what we're doing is here we're declaring a global scope variable and this variable is getting incremented to long before this code ever fires. In other words, we're trying to console log variable and we are doing so and we do it times digital marketing company in Noida but long ago this variable has been set to because the loop happens very very quickly. And therefore the variable we're referencing inside this console log is referencing the global scope variable. So again the loop has been completed long before we ever tried to do this at that time.

 

Now, what if we were to wrap this in a function. Well, you know what let's try it. So we'll say function Luper throw that all into a function. So all right let's try. We will invoke Luper. See what happens. Clear run the same thing right. So again this variable is scoped to this function and we are referencing the variable digital marketing company in Noida that has long ago been incremented. So I'm going to undo that because I don't really need it to be wrapped in a function at this time. So a couple of ways we can approach solving this problem. One thing which might be your instinct would be to just kind of grab out the variable and assign it to a new variable.

Verdict:

 

So if we say var the letter J comes after I say var J equals I then let's start a console log that letter up to the console log J run. And oh that's weird. Why is it a different number? And why is it just looping over and over again? So before it was because I got incremented and then it didn't enter the loop again. Here we don't get the final incrementation because we stop looping. In any case, this is the max number we're going to get. And this as well it's a variable.

 

And recall that variables are a function scoped. In this case, it's globally scoped. And again we're just referencing that global scope variable. So that's obviously not going to work. So how else can we approach this? Well you know what we can do we can close over this variable at any given time in terms of a digital marketing company in Noida executing the for loop and we can pass that into a separate function and this is the way that I usually solve this problem. And again it used to be the case I would need to write my code this way because I wasn't understanding what was happening but instead of doing directly inside the for loop Let's extract this out into a function. So let's say function Luper and it's going to take in some kind of value and we'll just move our set time out into this function so that time a function like so this one will also take a second.
 

bottom of page