Skip to main content

Posts

Javascript 106 : Promises

This post is a continuation of "Javascript 105: Callbacks". So If you have not studied the last post please go back and then come back to this.  As discussed in the last post, a callback is a way to force synchronous (sequential) operation of otherwise asynchronous tasks. Since this is a continuation lets begin from where we left off with the last post.  Take a look at this code, and its output.  Output:  We did that to ensure the statements prints in order This is okay for situation like this.  But imagine a code as follows Output: Take a look at the code and the output carefully. It is obvious that these statements are printing out of order due to the inherent asynchronous nature of JS.  If we still want to ensure that they take their own time to execute but still executing the way we want, how do you think that we can make this happen.  Sure, we can create multiple callback functions and call them inside of each (Which I even do not w
Recent posts

Javascript 105 : Callbacks

Javascript is asynchronous. That means you cant guarantee that the code you write will always execute sequentially. Take the following code for example, Output When looking at the output, it is understandable that the statements has been executing sequentially.  But if I do a modification to the code like the following, the output will be different.  Output:  Don't worry about the line number 6 yet. the amendment there will just delay the the execution of the statement "console.log('statement 01')" by 100 milliseconds. Now the "statement 01" prints out at the end of the line. The change of the output can be explained as follows. the JS interpreter sees the line 6 (which is the first line of the script), and realize that it has to wait for a 100 milliseconds before executing the statement "console.log('statement 01')", but sees that other four lines has no such delay and can be executed right away. So it goes a

Javascript 104 : Closures

Closures is an easy way of implementing encapsulation in Javascript.  Suppose you need to implement the calculation of a certain employee's increment scheme. Lets simplify it by saying that the annual increment depends on a certain performance index calculated out of a certain function.  Look at the following code and try to understand what is going on.   Output: I have included a fun function called "calculatePerformanceIndex". You can completely disregard it if you want to. But if you are still confused about what are functions, you should stop now and go to the previous post in which I have explained what functions are.  "calculatePerformanceIndex" is a function to calculate the performance based on efficiency, discipline and latency. This function is completely made up by me. So don't take it serious. What you should take serious is that it returns a value and this function has been called on line 9 and initialized the "perf

Javascript 103 : Functions

A function in javascript (or any high level language) is a  block of code segment which carry out a certain task. Once created, the function is not going to do anything by its own. You have to call it in your code use its functionality. You have to master functions and all other advanced forms of it which is coming up in the later posts in order to be a good developer because you can never avoid them and it is fundamental to any development work. Unless what you develop is a "Hello world!" program! Here's how you can declare a simple function. What I have here is a simple function called "doSomething", and it will just print "Hello world" on the console. But when you run this script, no error will show up, but nothing will print in the developer console either. That is because you have to call the function in order for it to come to life and start functioning. You can call the function like this, And it will give you the out put as t

Javascript 102 : Understanding 'this' keyword

The keyword "this" is frequently misunderstood when coding with Javascript. It is because its behavior depends on the execution context. Now what is "execution context"? It is mainly of three types. Global Execution Cotnext (GEC), Functional Execution Cotnext (FEC) and Eval. In GEC, it is the global environment which Javascript  (JS) is running. If you are running the script in a browser and you use the "this" keyword, it will refer to the "Window" object. But if you are using NodeJS and running the script outside a browser the "this" keyword would refer to the "Process". In FEC the "this" keyword would refer to the execution context created by the code inside the function. EVAL is hen you use the "this" keyword inside an "eval" function. If this does not make sense, don't worry because we are going to understand with an example which is always the best way. Make the following HT

Javascript 101 : var, let and const

Let's understand variable types "var", "let" and "const" in Javascript.  Basically there are 3 ways to create variables in java script using 3 keywords as follows.  There differences are as follows,  01. The "variable01" which was created with "var" keyword, does not have a blocked scope. which means, you have to be careful when using it. A variable declared inside an "if" block can be changed outside the block.  02. The "variable02" which was created with "let" keyword, has blocked scope. A variable with "let" keyword declared inside an "if" block is not available outside the block. 03. The "variable03" which was created with "const" keyword, is a constant. Which means you have to initialize it once you declare it, and that value cannot be modified later in the programs. There will be an error if you try to do so.  Now lets explore