Category

Javascript

ES6 – Class, Constructor, Super, Inheritance & Get/Set Stuffs!

ES6 is awesome. Why? It brings a new approach to code in Javascript. It has class and constructor so we can mimic OOP better. class Dog { constructor(name) { this._name = name; } get name() { return this._name; // returning a private variable _name } set name(newName) { let regex = new RegExp(‘^[a-zA-Z0-9.]*$’); if (regex.test(newName))…

Read More

What is “this” in Javascript?

What is “this” in Javascript – especially, when you use bind( ), arrow function and regular function?   this refers to its closest object. Maya, you’re not making any sense! Wait, let me explain.  If this is called in a global scope, then this refers to “window” object in a browser or “global” object in Node. function…

Read More

Functions in Javascript (Declaration, Expression, Arrow)

There are three ways of using functions and each has its own name, one is called “Function Declarations” and another is “Function Expressions”. The last one to be discussed is “Arrow Function”, which has the shortest syntax for a function. The Syntactical differences between them are like so : Function Declaration declared as a separate…

Read More

How To Use Reduce( ) in Javascript

[10,1000].reduce( (finalValue, whatToAddToFinalValue) => finalValue + whatToAddToFinalValue ); The array has 2 items [10, 1000], which means we run 2 iterations. On each iteration, reduces() takes 1 item ONLY! For the FIRST iteration, since we did not declare the finalValue starting value, so it will always be 0 : We have whatToAddToFinalValue variable, which will…

Read More

For VS While Loop in Javascript

When we need to perform similar actions for certain number of times, that is when we get to “Looping”. For example, when we need to output goods from a list one after another. Or just run the same code for each number from 1 to 10. Loops are a way to repeat the same part…

Read More

Semicolons in Javascript – Where and when?

Semicolon ; is placed at the end of an object declaration, assignment and a return statement. Without a semicolon, the code will be considered “undone”. So a statement will not be a statement, an assignment will not assign anything, a declaration will not declare anything. but wait, read this and this too. What? You are still gonna…

Read More

Javascript (Var vs Let vs Const)

VAR Var is functioned-scoped and variables defined in the function CAN NOT be accessed from its outer function. But variables can be accessed from its inner function. Children-scoped variables can USE its parents variables. Parents can not use its children variables – because … well, yeah, children like to keep secrets 😀 Var is hoisted…

Read More