Memoization is defined as “is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again”. To break this down, basically, what we want to do with memoization is try to speed up expensive function calls by caching previous results from the execution of the function call so that we are not performing the same calculations or operations over and over again.
Let’s take a look at a simple example, to start…
function addTo768(number) {
return number + 768;
}addTo768(10); // Outputs…
I’ve spend a couple of articles talking about falsy values in JavaScript (if you’re interested…NaN and Null and Undefined), and I wanted to turn things around and talk about truthy values in JavaScript.
To recap my previous articles, in JavaScript there are six values that are considered to be falsy, and they are false
, 0
, ‘’
, null
, undefined
, and NaN
. Now if you have, basically, any other value than one of these six, then there’s a good chance that value will be truthy. Now this does not mean that the value itself is equal to true
but that when the value is evaluated in boolean terms it will be coerced into true
. …
null
and undefined
are two of the six falsy values in JavaScript, the others being false
, 0
, ‘’
, and NaN
(I talked about NaN
here). Now null and undefined are similar but…different, we’re going to talk about why.
null
, according to MDN, “represents the intentional absence of any object value.” Which is basically saying that null
represents an empty value or nothing, we could have also explicitly set a value to null
, as such…
let nothingHere = "Nothing";nothingHere = null; // Removes "Nothing" and sets nothingHere to
nullnothingHere === null; // Outputs true
That’s pretty much it for null
, interesting side…
In JavaScript, the number type can be either an integer or a float, as such…
let integer = 10;let float = 10.5;typeof(integer) // Outputs numbertypeof(float) // Outputs number
Now, NaN
stands for “Not A Number”, and it means, well, whatever you’re looking at is not a number, that being said we can’t do something like…
let example = 'banana';typeof(whatever) // Outputs string
Because even though our example
variable is not a number, it is still a valid data type and JavaScript tells us what that type is. However, if we did something like this…
let example = 'banana' *…
An interesting and seemingly menacing sounding topic in JavaScript is the Temporal Dead Zone. Essentially, the Temporal Dead Zone refers to a behavior in JavaScript when variables are declared with the let
and const
keywords, and the space between where a variable enters scope and where it can be accessed is the Temporal Dead Zone. Let’s take a look at normal behavior for variable declaration…
let example = 10;
console.log(example); // Outputs 10
However, if we try to reverse the order…
console.log(example);
let example = 10; // ReferenceError: Cannot access 'example' before
initialization
In this example our console.log(example)
is in the Temporal Dead Zone, the example
variable has been declared and JavaScript knows that it exists, however, we cannot access the variable until it has been initialized with our value of 10
. If we tried the same thing with var, we would get a different…
In previous articles I’ve written the topics have included the idea of JavaScript closures before (although I never explicitly said that), and I thought that I would take this opportunity to break down closures a little bit and explain some more about them.
According to MDN, “A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time.”
To simplify this, basically, a closure is a set of nested functions where the inner function has access to variables from the outer function, even after the outer function has been executed. Let’s take a look at an example, to help visualize…
Curry is delicious, however, that is not the kind of curry that we are talking about when I say Currying in JavaScript. In JavaScript currying, refers to taking a function with multiple arguments and transforming it into a series of nested functions. Basically, each nested function is going to take care of the next argument from our original function, so we can keep adding as many arguments as we have. It’s like starting a dish with only one or two spices and then adding more and more spice to the dish, to make it as spicy as you like. …
As we edge closer to winter and the temperatures outside begin to get colder and colder, I bring you a relative topic in Javascript, Freezing Objects.
Objects in JavaScript are, essentially, containers for storing things, and in their natural state, we can always add new items into that container, change things in the container, or take stuff out of the container. Well, what if we want to put the lid on the container, so that nothing new can go inside it and nothing can come out? Now, your first thought may be to assign it with the const
keyword, and I can understand that line of thought, however, it doesn’t quite solve our problem. …
This week I present for your approval, JavaScript’s Immediately Invoked Function Expression (IIFE for short, pronounced “iffy”). An IIFE is a function that is invoked as soon as it is created, the upshot for this is that is does not create extra “stuff” in our global object.
To start let’s look at a normal, good old function…
function sayHi() {
console.log("Hello, World!);
}
Pretty straightforward, we’re creating a global function with the name of sayHi
and in order to invoke the function we have to call it as, sayHi()
, in our code, to get our Hello, World!
in the console.
We could also write our sayHi
function, as a function…
On my never-ending search of neat things in JavaScript, I bring you…Generators. A generator looks like a regular old function but acts like an iterator. Another neat thing about generators is that they can pause what they are doing and resume, where it was, it at a later time.
Let’s start out by just looking at a regular function…
function cannotBeStopped(){
console.log("Can't");
console.log("Stop");
console.log("Now");
}
If we run this function, it will execute from the top down giving us the output…
"Can't"
"Stop"
"Now"
Running it again will give us the exact same behavior, the function will execute from top to bottom, giving us the same output. …
About