avatarDr. Ashish Bamania

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

5450

Abstract

e</code> through a closure.</p><p id="8ed5">When we run <code>newFunction('inside')</code>, which is actually invoking <code>innerFunction</code>, it still has access to <code>outerVariable</code> even though <code>outerFunction</code> has already completed its execution.</p><div id="2bbe"><pre><span class="hljs-keyword">const</span> newFunction = <span class="hljs-title function_">outerFunction</span>(<span class="hljs-string">'outside'</span>);

<span class="hljs-title function_">newFunction</span>(<span class="hljs-string">'inside'</span>);
<span class="hljs-comment">// Output: outerVariable: outside, innerVariable: inside</span></pre></div><figure id="7256"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*eIR2lqF9QhtaoXB9"><figcaption>Photo by <a href="https://unsplash.com/@kvncnls?utm_source=medium&amp;utm_medium=referral">Kevin Canlas</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h1 id="b9b8">4. Prototype-based Inheritance</h1><p id="4cd9">In JavaScript, objects inherit properties from other objects.</p><p id="2af8">Every object in JavaScript has an internal property, <code>[[Prototype]]</code>, which is a link (reference) to another object.</p><p id="6070">The <code>[[Prototype]]</code> property of an object is set to the object that was used as its prototype when the object was created.</p><p id="0113">When we try to access a property that does not exist in the current object, JavaScript will use this link to access the property from its prototype.</p><p id="0d62">If an object’s prototype does not have a desired property, JavaScript will continue looking up the chain, checking the prototype’s prototype, and so on, until it either finds the property or reaches an object with a <code>null</code> prototype.</p><p id="083e">Let’s create two objects as below.</p><div id="525a"><pre><span class="hljs-keyword">let</span> animal = { <span class="hljs-attr">eats</span>: <span class="hljs-literal">true</span>, };

<span class="hljs-keyword">let</span> rabbit = { <span class="hljs-attr">jumps</span>: <span class="hljs-literal">true</span>, };</pre></div><p id="8375">Next, we set the prototype of <code>rabbit</code> to <code>animal</code>.</p><div id="13df"><pre>rabbit.<span class="hljs-property">proto</span> = animal; </pre></div><p id="f529">This can also be done as below(<b><i>better way</i></b>):</p><div id="3563"><pre><span class="hljs-keyword">let</span> animal = { <span class="hljs-attr">eats</span>: <span class="hljs-literal">true</span> };

<span class="hljs-keyword">let</span> rabbit = <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">create</span>(animal);

rabbit.<span class="hljs-property">jumps</span> = <span class="hljs-literal">true</span>;</pre></div><p id="7d22">Now, we can access the properties of the <code>animal</code> object from the <code>rabbit</code> object.</p><div id="8afa"><pre>console.<span class="hljs-built_in">log</span>(rabbit.eats);

<span class="hljs-comment">//Output: true</span></pre></div><p id="f25f">Note that JavaScript classes, introduced in ES6, is a syntactic sugar over prototypal inheritance. Under the hood, it is the same process of objects inheriting properties and methods from other objects.</p><h1 id="3500">5. Hoisting</h1><p id="85af">Hoisting in JavaScript is a mechanism where variable and function declarations are moved to the top of their containing scope during the compilation phase before the code has been executed.</p><h2 id="3927">Variable Declarations Are Hoisted & Not Their Initializations</h2><p id="ab5b">Check out the example below.</p><div id="9ebc"><pre><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myVar); <span class="hljs-comment">// undefined</span>

<span class="hljs-keyword">var</span> myVar = <span class="hljs-number">5</span>;

<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myVar); <span class="hljs-comment">// 5</span></pre></div><p id="3fea">In this example, in the first line, accessing <code>myVar</code> returns <code>undefined</code> rather than throwing a <code>ReferenceError</code>.</p><p id="5693">This is because the variable declaration (<code>var myVar</code>) is hoisted to the top of the scope.</p><p id="7a49">However, the initialization (<code>myVar = 5</code>) is not hoisted.</p><p id="6b08">This looks like the following after hoisting:</p><div id="3a22"><pre><span class="hljs-keyword">var</span> myVar; <span class="hljs-comment">//Hoised on top at compile time</span>

<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myVar); <span class="hljs-comment">// undefined</span>

myVar = <span class="hljs-number">5</span>;

<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myVar); <span class="hljs-comment">// 5</span></pre></div><h2 id="5b36">Functions Declarations Are Hoisted & Not Their Initializations</h2><p id="7951">Let’s check out an example.</p><div id="e20b"><pre><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">myFunction</span>()); <span class="hljs-comment">// "Hello World!"</span>

<span class="hljs-keyword">function</span> <span class="hljs-title func

Options

tion_">myFunction</span>(<span class="hljs-params"></span>) { <span class="hljs-keyword">return</span> <span class="hljs-string">"Hello World!"</span>; };</pre></div><p id="c01a">This works well because the whole function definition is hoisted to the top when using <b>Function Declaration </b>(with the <code>function</code> keyword).</p><p id="5f24">Let’s see what happens we define a function using <b>Function Expression</b> as below.</p><div id="a670"><pre><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title function_">myFunction</span>()); <span class="hljs-comment">// TypeError: myFunction is not a function</span>

<span class="hljs-keyword">var</span> myFunction = <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) { <span class="hljs-keyword">return</span> <span class="hljs-string">"Hello World!"</span>; };</pre></div><p id="0ae3">In the above, <code>var myFunction</code> is hoisted, but the assignment of the function to <code>myFunction</code> is not. Thus, we get a <code>TypeError</code> when executing the first line.</p><h2 id="7c7e">What about when using ‘let’ and ‘const’?</h2><p id="63c9">The <code>let</code> and <code>const</code> keywords in ES6 also hoist their declarations to the top of the block, but accessing them before the declaration results in a <code>ReferenceError</code>.</p><h2 id="1e0d">Subscribe to my free Substack newsletters below:</h2><div id="f004" class="link-block"> <a href="https://ashishbamania.substack.com/"> <div> <div> <h2>Ashish’s Substack | Dr. Ashish Bamania | Substack</h2> <div><h3>Sharing Everything That I Have Learned & Have Been Learning About, Unfiltered. Click to read Ashish’s Substack, by Dr…</h3></div> <div><p>ashishbamania.substack.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*0muO09Ou9fuuOy6L)"></div> </div> </div> </a> </div><div id="9a14" class="link-block"> <a href="https://bytesurgery.substack.com/"> <div> <div> <h2>Byte Surgery | Dr. Ashish Bamania | Substack</h2> <div><h3>A Deep Dive Into The Best Of Software Engineering. Click to read Byte Surgery, by Dr. Ashish Bamania, a Substack…</h3></div> <div><p>bytesurgery.substack.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*MV8XJJxLS50pwwv7)"></div> </div> </div> </a> </div><h2 id="af29">Check out my books below:</h2><div id="6782" class="link-block"> <a href="https://bamaniaashish.gumroad.com/l/python-book"> <div> <div> <h2>The No Bullst Guide To Learning Python</h2> <div><h3>Are you someone who is thinking about learning to program but does not know where to start? </h3></div> <div><p>bamaniaashish.gumroad.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*IzmR1fn8tnDUwtMW)"></div> </div> </div> </a> </div><div id="b228" class="link-block"> <a href="https://bamaniaashish.gumroad.com/l/ai_book"> <div> <div> <h2>The No Bullst Guide To Learning Artificial Intelligence</h2> <div><h3>Hey there! Are you someone who wants to learn about AI but does not know where to begin?</h3></div> <div><p>bamaniaashish.gumroad.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*f2NnQeA-HCVPwdTM)"></div> </div> </div> </a> </div><div id="c790" class="link-block"> <a href="https://bamaniaashish.gumroad.com/l/html-book?layout=profile"> <div> <div> <h2>The No Bulls**t Guide To Learning HTML</h2> <div><h3>Tired of lengthy, complicated tutorials? Want to dive straight into the core concepts and start building websites like…</h3></div> <div><p>bamaniaashish.gumroad.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*b3LF1o5vYcHQACfU)"></div> </div> </div> </a> </div><div id="ddba" class="link-block"> <a href="https://bamania-ashish.medium.com/membership"> <div> <div> <h2>Join Medium with my referral link — Dr. Ashish Bamania</h2> <div><h3>Read every story from Dr. Ashish Bamania (and thousands of other writers on Medium). Your membership fee directly…</h3></div> <div><p>bamania-ashish.medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*Bk4zPehgusbJoPFe)"></div> </div> </div> </a> </div></article></body>

These Are The Top 5 Toughest Concepts in JavaScript According To GPT-4

Photo by Juanjo Jaramillo on Unsplash

1. Asynchronous Programming

JavaScript is single-threaded, but it has features for asynchronous operations which allow it to handle time-intensive/ compute-intensive tasks (like reading files & making network requests) without blocking the rest of the program.

JavaScript does this with the help of:

  • Callback functions
  • Promises
  • ‘Async/ await’ syntax (built on top of Promises)

To learn Asynchronous Programming, check out these articles from my upcoming book ‘The No Bulls**t Guide To Learning JavaScript’.

2. Event Loop and Concurrency Model

JavaScript has a single-threaded, non-blocking, asynchronous, concurrent runtime, which is primarily implemented through an event loop and a task queue.

Understanding how these work together to handle JavaScript’s execution can be a particularly challenging aspect of the language.

Check out my article which explains this in detail from scratch:

3. Closures

A closure gives you access to an outer function or parent function’s scope from an inner/ child function, even after the parent function has finished its execution.

Check out the function below:

function outerFunction(outerVariable) {
    return function innerFunction(innerVariable) {
        console.log('outerVariable:', outerVariable);
        console.log('innerVariable:', innerVariable);
    };
};

In the above example, innerFunction is a closure.

It is defined inside outerFunction and has access to outerFunction's variables and parameters (outerVariable).

Even after outerFunction has finished execution, innerFunction still retains access to outerVariable through a closure.

When we run newFunction('inside'), which is actually invoking innerFunction, it still has access to outerVariable even though outerFunction has already completed its execution.

const newFunction = outerFunction('outside');

newFunction('inside');  
// Output: outerVariable: outside, innerVariable: inside
Photo by Kevin Canlas on Unsplash

4. Prototype-based Inheritance

In JavaScript, objects inherit properties from other objects.

Every object in JavaScript has an internal property, [[Prototype]], which is a link (reference) to another object.

The [[Prototype]] property of an object is set to the object that was used as its prototype when the object was created.

When we try to access a property that does not exist in the current object, JavaScript will use this link to access the property from its prototype.

If an object’s prototype does not have a desired property, JavaScript will continue looking up the chain, checking the prototype’s prototype, and so on, until it either finds the property or reaches an object with a null prototype.

Let’s create two objects as below.

let animal = {
  eats: true,
};

let rabbit = {
  jumps: true,
};

Next, we set the prototype of rabbit to animal.

rabbit.__proto__ = animal; 

This can also be done as below(better way):

let animal = {
  eats: true
};

let rabbit = Object.create(animal);

rabbit.jumps = true;

Now, we can access the properties of the animal object from the rabbit object.

console.log(rabbit.eats);

//Output: true

Note that JavaScript classes, introduced in ES6, is a syntactic sugar over prototypal inheritance. Under the hood, it is the same process of objects inheriting properties and methods from other objects.

5. Hoisting

Hoisting in JavaScript is a mechanism where variable and function declarations are moved to the top of their containing scope during the compilation phase before the code has been executed.

Variable Declarations Are Hoisted & Not Their Initializations

Check out the example below.

console.log(myVar); // undefined

var myVar = 5;

console.log(myVar); // 5

In this example, in the first line, accessing myVar returns undefined rather than throwing a ReferenceError.

This is because the variable declaration (var myVar) is hoisted to the top of the scope.

However, the initialization (myVar = 5) is not hoisted.

This looks like the following after hoisting:

var myVar; //Hoised on top at compile time

console.log(myVar); // undefined

myVar = 5;

console.log(myVar); // 5

Functions Declarations Are Hoisted & Not Their Initializations

Let’s check out an example.

console.log(myFunction()); // "Hello World!"

function myFunction() {
  return "Hello World!";
};

This works well because the whole function definition is hoisted to the top when using Function Declaration (with the function keyword).

Let’s see what happens we define a function using Function Expression as below.

console.log(myFunction()); // TypeError: myFunction is not a function

var myFunction = function() {
  return "Hello World!";
};

In the above, var myFunction is hoisted, but the assignment of the function to myFunction is not. Thus, we get a TypeError when executing the first line.

What about when using ‘let’ and ‘const’?

The let and const keywords in ES6 also hoist their declarations to the top of the block, but accessing them before the declaration results in a ReferenceError.

Subscribe to my free Substack newsletters below:

Check out my books below:

Programming
Software Development
JavaScript
Web Development
Artificial Intelligence
Recommended from ReadMedium