Learn Hoisting in Javascript and Functions from an online marketing company

Learn Hoisting in Javascript and Functions from an online marketing company



It did the logic for us. Right, so we’re doing two functions. First of all, we run one function then we run another and then down here when we cancel log We actually just check you know we run another function and we get back the return value of adding those two numbers up. Now I did want to briefly touch on the concept of hoisting and we don’t need to know too much about it but we just need to understand what happens in terms of declaring functions and what hoisting means and hoisting basically means when the code is compiled all the functions are basically placed at the top of the code. And that means you don’t have to always declare your functions right off the bat. So let’s say I didn’t declare this function here instead I just declare a couple of constants and I put my function you know in my online marketing company down at the bottom where you might expect this not to work. But you know let’s run it and let’s see what happens. OK, so it still works.

And that means that we don’t really have to worry about where our functions are placed. In our code, they’re hosted by the compiler basically to the top of the code behind the scenes. You don’t really need to worry about it. Same with this one as well. So this one I could have put anywhere really down at the bottom or up at the top doesn’t matter. We don’t have to define the function before invoking it when we define functions this way. You know it might be confusing if you don’t have your functions declared right off the bat but it’s not uncommon to see that because javascript does it for us to the top of the code. And you know what for the sake of clarity I’m going to move this console log down to the bottom like so. And as we see it still runs perfectly fine online marketing company.

 And this way I can just do a console log instead of just directly doing the math in the console log. And here we invoke number after. Number one we want to use is to double and the other parameter is no doubled for example. So we’re invoking this function passing it into things that we know are numbers because we already checked the type of. And we are able to add them up. And let’s see what happens. Clear We’ll run it. Hey pretty cool. Let’s see getting back a number. Oh OK. So it’s a number which means I can do something like No. doubled plus five doubled if you wanted to. It looks like it works. You can also make another function to kind of take these and add them together if you wanted to. So let’s go ahead and do that function. You can kind of declare it wherever you see fit.

No the way that we can kind of assign the return value or function just assigns it to like a variable or cons or let for example. So if we say online marketing company lets I don’t know what to call this number doubled or number two doubled for example and we make this equal to the return value of the function and we do that by saying Konst No. doubles equal number doubler with the number two. On the topic of hosting something, we do need to be aware of if you're writing something that's what's known as an impure function. In other words, you're accessing a variable or a constant outside of the scope of the function itself. Right so if you have a variable you directly access that variable inside a function to change the value, for example, that's what's known as an impure function. In other words, the de-value is not passed in you just directly access it and due to the nature of hoisting this might result in some unexpected behavior.

And so it's kind of a common gotcha for those especially those that are just kind of learning javascript and to show an example of an online marketing company. Let's say we have a contest declared here Konst my name equals Chris and we'll write another function here that will basically say hi. So we'll do a function say hi again say Hi it's not going to take in any parameters and it's just going to console log something out so high plus my name. Right, so we know that this will work if we invoke the function like saying hi. This has been hoisted but it still has access to the constant just out of space here. Now if you were doing something like this so if you threw in an IF statement here. So if there is no my name for example and here inside my block I'm going to actually use a variable because I want this variable scoped to function and when to use the same name my name equals Bob. So what do you expect concern log will show here.

You'd expect it to show Chris because we checked my name. We checked if it exists. This will return true only if there is no name right. This is a not check. So if my name is nil or undefined then this block of code will run. So this technically shouldn't be running right we should just skip over this console log my name. Let's check it out. Run we see. And that's why you use a variable because I'm accessing this online marketing company as variable outside of the scope of this if statement. Now that's kind of weird right. And it's because this is being hoisted. So if you were to look at this you know the compiled code it would look more like this. And it's because we are trying to access a variable that's kind of hasn't been defined yet. It does work if we remove this because of its kind of compiles it in a way that it understands.

If we do declare this later. But that's just something to be aware of. So this is as I said kind of a common gotcha when you're learning javascript and it's just something to be aware of. It's usually not a big issue but if you're having unexpected behavior in your functions you know chances are it might be always seeing the error and it just kind of tells you that maybe you should rewrite your function to not be impure right. Maybe you should read more of a pure function and we should be passing in an argument here. If we did it this way. My online marketing company and then we do the check. If there's no my name then my name is that. And then we can say you know we just pass it in a string like this. So let's see what happens here if we run it. That's how it works as expected because we're directly passing in the parameter.

Report Page