Categories
javascript

Beginner JavaScript – 2 – How JS Code is Executed in the browser ?

✨Beginner JavaScript – 2 – How JS Code is Executed in the browser?

Hey everyone 👋🏻,

In this article, let us discuss about how JS Code is Executed in the browser. This is the second part of my Beginner JavaScript Series on Dev.

How JS Code is Executed inside the browser ?

The source code that you and I write is first passed through a program called a compiler, which in turn translates it into something called as the bytecode which the machine can understand and execute.

Contrary to this, JavaScript has no compilation step. Instead, an interpreter in the browser goes over your entire JavaScript code, interprets it line by line, and then runs it.
Some of the more modern browsers use a technology known as Just-In-Time compilation, which compiles JavaScript to executable bytecode just when it is about to run.

Alt Text

Alt Text

Check this video where I explain How JavaScript Code is Executed in Browser

So this is it for this article. Thanks for reading.
Don’t forget to leave a like if you loved the article. Also share it with your friends and colleagues.

Categories
javascript

Beginner JavaScript – 1 – The Language and How Web Works ?

✨Beginner JavaScript – 1 – The Language and How Web Works ?

Hey everyone 👋🏻,

In this article, let us discuss about the JavaScript Language and How the Web Works. This is the first part of my Beginner JavaScript Series on Dev.

The Language and How the Web Works

JavaScript is a powerful, event-driven, dynamic and flexible programming language. It can execute on a web browser that allows us to make interactive webpages such as popup menus, animations, form validation.

So the moment you enter the URL address in your browser and hit ENTER, some things happen under the hood. Let us understand them.

Alt Text

  1. The first thing that happens is the resolution of the URL.
  2. An HTTP request is sent to the server of the website.
  3. The response of the server is then parsed.
  4. Finally the page is rendered and then displayed.

Check this video where I explain the Language and How the Web Works:

So this is it for this article. Thanks for reading.
Don’t forget to leave a like if you loved the article. Also share it with your friends and colleagues.

Categories
binary trees data structures javascript

Data Structures Problem – Checking for Children Sum Property in a given Binary Tree 🌳

✨Data Structures Problem – Checking for Children Sum Property in a given Binary Tree 🌳

 

Hey guys 👋🏻,

I just released a new video on solving an interview problem :

Checking for Children Sum Property in a given Binary Tree

In this video, we will solve the problem of checking whether the given binary tree satisfies the children sum property or not.
We will first understand the approach that we will be taking to solve this problem and then we will write the code for the same problem together. Once we are done with the implementation, we will do a quick dry run for the code to understand and make complete sense of the output that we get. Here is the code from that video : 

// Children Sum Property in a binary tree

// We need the class for a node first
class Node {
  constructor(data) {
    this.data = data;
    this.left = null;
    this.right = null;
  }
}
let rootNode;

function isChildrenSumPropertySatisfied(node) {
  let leftChildData = 0;
  let rightChildData = 0;

  if(node === null || (node.left === null && node.right === null)) {
    return true;
  }else {
    leftChildData = node.left !== null *
  }
}

function isChildrenSumPropertySatisfied(node) {

  let leftChildData = 0;
  let rightChildData = 0;

  if (node == null || (node.left == null && node.right == null)) {
    return true;
  } else {
    if (node.left != null) {
      leftChildData = node.left.data;
    }
    if (node.right != null) {
      rightChildData = node.right.data;
    }

    if ((node.data == rightChildData + leftChildData) &&
      (isChildrenSumPropertySatisfied(node.left) != false)
      && (isChildrenSumPropertySatisfied(node.right) != false)) {
      return true;
    }else {
      return false;
    }
  }

}
const root = new Node(10);
root.left = new Node(8);
root.right = new Node(2);
root.left.left = new Node(3);
root.left.right = new Node(5);
root.right.right = new Node(2);

if (isChildrenSumPropertySatisfied(root) != 0) {
  console.log('The given tree satisfies the children sum property');
} else {
  console.log('The given tree does not satisfies the children sum property.');
}

So this is it for this article. Thanks for reading.
Don’t forget to leave a like if you loved the article. Also share it with your friends and colleagues.

Alt Text
Categories
javascript web development

Where to filter the data – backend or frontend ?

✨Where to filter the data – backend or frontend ?  

 

Hey everyone,

In this article, let us discuss about a very important question regarding filtering the data that we get from the database or API. So the question is : 

Where exactly should we filter the data ? 

🤔 Backend … ? 

🤔 Frontend … ? 

Well, the answer is : 

It depends. 

Let us discuss now : 

So if you have a a large amount of data, it is better to implement the filtering logic on the backend and let the database handle all the heavy lifting for us. Contrary to this, if you are dealing with less amount of data, you can do the filtering logic on the frontend.

In general, if you are working with a million recordset (as an example) and there are tons of users trying to access those records at the very same, then in such cases it does not make sense to send a million recordset to every user who is currently accessing the website. It simply does not make sense because it will simply bring your website down, so what we do in such cases is that we setup a record limit (kind of we send the data in batches) by using a features like database cursors. So kind of with the cursors in place, you can fetch the data in batches in some sort of pagination etc. Consider a big ecommerce website like Amazon, this is how they handle that amount of data in a nutshell.

For less amount of data, you could retrieve the data from the backend and then use the filtering logic on the frontend.

So this is it for this article. Thanks for reading.
Don’t forget to leave a like if you loved the article. Also share it with your friends and colleagues.

Alt Text

Categories
html javascript vuejs web development

✨Top 5 resources to learn Vue.js [in no particular order]

✨Top 5 resources to learn Vue.js

Hey everyone 👋🏻,

In this article, I will be mentioning the top 5 resources that I highly recommend if you want to learn Vue.js.

PS : I am not an affiliate of any of these resources/courses. These are just some of my personal recommendations if you are looking to learn Vue.

Introduction

Alt Text

Vue is a progressive framework for building user interfaces. Unlike other monolithic frameworks, Vue is designed from the ground up to be incrementally adoptable. The core library is focused on the view layer only, and is easy to pick up and integrate with other libraries or existing projects.

Here are the top 5 recommendations (in no particular order) of the courses that you should definitely check out for learning Vue.

1. Build Web Apps with Vue 3 and Firebase by Shaun Pelling a.k.a The Net Ninja (rated 4.8)

image

Shaun is a gifted teacher. He knows how to explain stuff in a very easy and lucid manner. This course will definitely take your knowledge to the next level if you are looking to learn this specific technology.

Here is what this course covers

Learn how to create Vue 3 applications from the ground-up

Get in-depth knowledge of Vue features like the Vue Router, Vue CLI, Options API, Composition API, Teleport etc

Get hands-on & in-depth experience using the latest Vue 3 features (such as the Composition API)

Learn how to use Firebase as a back-end to your Vue applications (as a database, authentication service, hosting etc)

Build & deploy 4 real-world web apps with Vue & Firebase
Learn how to implement an authentication system into your Vue js apps using Firebase Auth

Become a Vue 3 ninja!

Course Link :

https://www.udemy.com/course/build-web-apps-with-vuejs-firebase

2. Vue.js – The Complete Guide by Maximilian Schwarzmuller ( rated 4.8)

image

This course as the name can probably tell you covers the internals of Vue in great detail. The course starts from the very basics like What Vue.js is and how it works and later on moves to more complex and advanced topics

Here is what this course covers

What is VueJS and Why would you use it?

The Basics (including the basic Syntax, Understanding Templates and much more!)

How to Output Reactive Data & Listen to Events

Interacting with DOM (Rendering Lists, Conditionally attaching/ detaching Elements …)

Setting up a Development Environment and Workflow

Using Components (and what Components are to begin with)

A Deep Dive Look Behind the Scenes of Vue

Binding to Form Inputs

Sending Http Requests to Backend APIs

Authentication & Vue

How to make your App more Beautiful with Animations and Transitions

How to create an awesome Single-Page-Application (SPA) with Routing

How to improve State Management by using Vuex

How to Deploy the App

and more …

Course Link :

https://www.udemy.com/course/vuejs-2-the-complete-guide

3. Vue Mastery

This is a complete path for learning Vue.js from some prominent industry experts pertaining to the technology.

image

image

Here is what this course covers

Their courses are distributed in form of paths. You may want to try them out as well.

image

Course Link :

https://www.vuemastery.com/courses

4. VueSchool.io

image

Here is what this course covers

image

You may want to try their complete suite of courses to enhance your Vue.js Knowledge. So down below, you can find the link for their website :

Course Link :

https://vueschool.io/courses

5. Vue.js Documentation (a must thing to refer)

image

Docs link :

https://v3.vuejs.org/

So this is it for this article. Thanks for reading.
Don’t forget to leave a like if you loved the article. Also share it with your friends and colleagues.

Categories
ejs html javascript mongodb nodejs template engines web development

Rendering Dynamic Content and Template Engines

✨Rendering Dynamic Content and Template Engines

Hey everyone 👋🏻,

In this article, let us learn about how we can render dynamic content to the client (which is not static). Let us first touch briefly on what a template engine is because rendering dynamic content is closely related to what a template engine will help us in achieving.

Template Engines – a brief overview

Image description

A template engine is software designed to combine templates with a data model to produce multiple pages that share the same look throughout the site. These are the views in an MVC project.

So this helps us in putting dynamic content on our HTML pages.

We got a HTML like template (technically not HTML) which is typically a file that contains a lot of HTML like content in it but with some placeholders plugged into it and then we have a server (a Node.js Express server or any other server) serving the Node/Express content and then you have a templating engine.
that replaces placeholders with HTML content but that content is generated on the server dynamically on the fly taking that dynamic content into account.

Some of the common template engines :

1. EJS

EJS Uses normal html and plain javascript in your templates.

Image description

EJS provides us clean syntax that easily mixes with HTML and provides the flexibility to write the JavaScript code inside the templates.

Dynamic content to be rendered in the above example is name. The value of the name variable will be rendered as an HTML page’s paragraph with value of name getting rendered on the fly.

2. Pug

PUG uses minimal HTML and custom template language.

Image description

3. Handlebars

Handlebars uses normal HTML and custom template language

Image description

The side effect of Handlebars template compared to Pug is that we cannot run any logic in the Handlebars template we need to pass a variable that has a value true or false(i.e logic is performed outside and passed into hbs template) and then we can render dynamic content.

This keeps our templates lean and all our logic should live while making sure that all our logic lives in our backend code. It works on the philosophy of less logic in the template and more logic in the node express code.
So you have to prepare everything there so that inside the template you don’t have to write any JavaScript expressions.

So this is it for this article. Thanks for reading.
Don’t forget to leave a like if you loved the article. Also share it with your friends and colleagues.

Categories
css html javascript mongodb nodejs web development

(2021) – Web Developer Full Course : HTML, CSS, JavaScript, Node.js and MongoDB

✨Web Developer Full Course

Alt Text

I recently started to create a Full Web Developer Course on my YouTube Channel and recently finished with the HTML and the CSS Section of this FREE course. More videos will be up on upload.

HTML Section

CSS Section

JavaScript Section

This section covers the basics of JavaScript along with the modern features that the language offers. We cover concepts like :

  1. JavaScript Basics (variables, loops, conditionals, functions)
  2. JavaScript Types (Strings, Numbers, Objects, Booleans, Functions, Symbols)
  3. Immediately Invoked Function Expressions
  4. Document Object Model (DOM) – Querying, Traversal, Inserting and Removing Elements from the DOM
  5. Arrays and Iterables
  6. Mutator methods – push, pop, shift, unshift, splice
  7. Other array methods like – slice, indexOf, findIndex, find, sort, reverse
  8. Higher Order Methods – filter, reduce, map
  9. Split and Join Methods
  10. The spread operator
  11. Sets and Maps
  12. Understanding the this keyword in JavaScript
  13. Overview of Objects, Constructor Functions, Classes, OOP, Instance and Static Methods, Inheritance
  14. Events – Adding and Removing Event Listeners
  15. Event Capturing – Bubbling Phases, Propagation and Event Delegation
  16. Asynchronous JavaScript – Event Loop (Call Stack, Message Queue)
  17. Asynchronous JavaScript – Understanding Promises and Error Handling
  18. Asynchronous JavaScript – Promise.race, Promise.all, Promise.allSettled
  19. Demystifying Async Await, From Promises to Async Await
  20. Scopes in JavaScript, Closures
  21. Symbols in JavaScript
  22. Iterators and Generators in JavaScript
  23. The Concept of Pure and Impure Functions in JavaScript
  24. Understanding Hoisting in JavaScript

AND MORE….

A lot of exercises and projects were made in this section of the course.

Here are the Projects that we made in this JavaScript Section :

Building COVID-19 Tracker Application using JavaScript and Mapbox

Making a Todo List Application in JavaScript using Custom Events

Implementation of Infinite Scroll in JavaScript

10 JavaScript Projects in 2 Hours 

Node.js Section
COMING SOON

MongoDB Section
COMING SOON

Make sure to subscribe to the channel for more such videos.
A lot of AWESOME videos are coming this month… So make sure to SUBSCRIBE to the channel for more amazing videos.

Follow me on Twitter for more updates: https://twitter.com/The_Nerdy_Dev

Categories
javascript reactjs

What is REST API ?

✨What is REST API ? 

Alt Text

Hey everyone ??,

In this article, let us learn about one of the most important concepts that you as a backend developer must have a solid knowledge of (because anyways working with an API is a big part of your jobs) and that is REST APIs

REST APIs

Cover image for What is a REST API ?

Actual definition:

“”
Representational state transfer (REST) is a software architectural style that was created to guide the design and development of the architecture for the World Wide Web. REST defines a set of constraints for how the architecture of an Internet-scale distributed hypermedia system, such as the Web, should behave. The REST architectural style emphasises the scalability of interactions between components, uniform interfaces, independent deployment of components, and the creation of a layered architecture to facilitate caching components to reduce user-perceived latency, enforce security, and encapsulate legacy systems.
“”

Now you must be wondering : “What the heck does this mean ?”

Alt Text

Anyways, let us make it super simple and understand what a REST API is piece by piece.

Now before we understand why we use a REST API, we first need to understand what problems does it solve ?

REST APIs are there to solve one problem you can say that not every frontend (UI) requires HTML pages. Not every UI wants your server code to generate HTML pages which effectively is the UI.
Think about mobile apps these apps typically do not work with server side rendered HTML code. They do not need a templating language on server side to render the HTML code because you use a rich suite of prebuilt libraries for UI provided by Google or Apple to build your UI in the respective IDEs of these programming languages.

Alt Text

You build these User Interfaces totally decoupled from your server side logic. You don’t want HTML code because you can’t really render it there. Obviously you have browsers in your phone and that will render HTML pages.

But most of the applications do not use HTML to build their user interfaces but the tools that are provided by Google or by Apple and then you only need data to fill these user interfaces to life.

For building these amazing and sleek looking user interfaces on the frontend we have frontend technologies like React, Angular, Vue etc.

Alt Text

So we have a frontend that is decoupled from a certain backend logic like Google Maps (Server) and we only exchange data because we don’t need the HTML code but just the data. We built the User Interface on our own. So we just need a backend server that can typically serve us just the data that we need and then we can easily fabricate our user interfaces as per our requirements.
So, this in essence is the core idea behind the REST APIs.

Alt Text

REST stands for Representational State Transfer which means
that we transfer data (some form of state you could say) instead of user interfaces. We just want our server to send us data and we leave it to our client side app be it a mobile application or be it a single page application to do whatever it wants to do with this data. Thus in traditional web applications we rendered the views on the server side using templating engines and handled both the data and the UI . This is not bad at all it is a common pattern for a lot of web applications

Traditional Web Apps and REST APIs – ONLY THE RESPONSE DATA DIFFERS

Alt Text

Often traditional web applications and the REST APIs are seen as totally two different things. They are not, they only differ in the response and the kind of data that you expect but they don’t differ at the server besides the fact that you do not render
the view there on the server. Here we use all knowledge that we gathered we only fine tune the data handling and finally deliver it to the client in the format in which it is required by the client.

A MAGNIFIED AND CRYSTAL CLEAR PICTURE OF THE REST APIs

Alt Text

So consider this scenario where we have our client (a mobile app, SPA) and then we have our server on which we build our backend REST API.

Now one possible advantage of this can be that we can make one and the same API for handling multiple clients and this is possible because both our mobile apps and web apps rely on the same data source, right. In fact, both the apps are same and consume the same data source. The only difference could be on how they present this data in their user interfaces.

Contrary to this, we can also have kind of a traditional application which just needs our service APIs. And in all such applications it is the data which is exchanged between the client and the server.

WHAT FORMAT DO WE EXCHANGE THE DATA IN

Some of the common formats that we most likely see are :
1.HTML
2.XML
3.JSON etc.

So there are different kinds of data that we can attach to the request and the response objects. So we can send plain text, XML or we could even send JSON.

There are different kinds of data that we can attach to the
request and the response objects. We can send plain text,XML or we could send JSON.

XML

Alt Text

Introduction

React is a free and open-source front-end JavaScript library for building user interfaces or UI components. It is maintained by Facebook and a community of individual developers and companies. React can be used as a base in the development of single-page or mobile applications. In this article, let us see some cool projects that you can make to make the process of learning React extremely easy.

It looks a lot like HTML. XML allows us to use any tags. It allows us to send any data and does not make assumptions about the user interface because it is not parsable by browsers. They are user defined elements.
These are machine readable but relatively verbose but you can define clear structures here. Here an XML parser is needed because node traversal in XML node tree is kind of a challenge in itself. So in essence you need an XML parser to efficiently parse the XML data.

JSON

Alt Text

JSON is kind of a defacto standard for data interchange in web applications. Here also we work on just the data and the JSON does not make assumptions about the user interface. It is machine readable and a bit more concise than XML and can easily be converted to Javascript.
This is a huge plus when working with Node.js on the server and JavaScript on the browser. It is the most common format used in any of the APIs that you are communicating with these days.

Routing and HTTP methods

Now we know our web works on request and response cycle. So we have a client that interacts with the server in form of requests and response to get the data that it needs and more. Check my video on Client Server Architecture to know more regarding this :

So this is how we communicate with our server. In the REST World, these communications take place in form of paths which are known as API Endpoints

API ENDPOINT = ANY COMBINATION OF THE HTTP METHOD AND THE RESPECTIVE PATH
These are the endpoints that we define on our REST API and we
also define some logic that should get fired when such a request reaches our end point.

HTTP METHODS

More than just GET AND POST(These are only when we are working with links and forms), we have other HTTP methods as well that the browser natively knows of. So here is a short summary of the HTTP methods that our request client has access to.

GET – Get a resource from the server

POST – Post a resource to the server which is responsible for creating or appending a resource to an already existing array of resources so to say.

PUT – Put a resource onto the server(i.e create or overwrite a resource). Post should never overwrite an existing resource though it can do but it is not recommended

PATCH – Update parts of the existing resource on the server(not overwriting the entire resource but the parts of it)

DELETE – Delete a resource on the server

OPTIONS – Determine whether follow up request is allowed(sent automatically by the browser)
This is automatically sent by the browser to find if the next request it tries to do is allowed or not.
It is the best practice to use these methods in this way though you can do anything you want

This way any one who uses your API clearly knows what to expect for a given method

REST PRINCIPLES

Alt Text

1. Uniform interface, clearly defined API endpoints with clearly defined request and response structures
IOW, your API should be predictable and also it should be well documented if it is open to the public.
So people should know what data your API expects and what data it gives back.

2. Stateless interactions
When building the server and the client, the server and the client are totally decoupled and they don’t share a common history
,every request is handled separately. And therefore no sessions can be used since no connection history is shared among the client
and the server. Every incoming request is treated as if no prior request was sent. Server has a look on every request on its own. It does not store a session about the client.

You can just build an API and open it to the public and just present the documentation of its usage to the client.
You don’t care at all about the clients. You just say here is the endpoints I have ,here is the data you get for each of the well-defined endpoints.
So in essence, we have a strong decoupling between the client and the server even if they were to run on the same server because we are building the API for our application so that they work independently from each other and just exchange data without sharing the connection history.

3.Cacheable

Alt Text

You could send the client some response headers telling the client for how long the response is valid.
Servers may set caching headers to allow the client to cache responses.

4.Client-Server
Client and Server are separated,client is not concerned with persistent data storage.It is the responsibility of the server.

5.Layered System

Alt Text

As a client we send a request to a server, we can’t rely on that server we sent it to to immediately handle the request, the server may handle the request to other server or distribute it to other servers if it is busy.
Ultimately we care about the data that we get back which should follow the structure that was defined by the API.

6.Code on Demand

Alt Text

REST API could also for some end points transfer executable code to the client.

So this is it for this article. Thanks for reading.
Don’t forget to leave a like if you loved the article. Also share it with your friends and colleagues.

REST API could also for some end points transfer executable code to the client.

So this is it for this article. Thanks for reading.
Don’t forget to leave a like if you loved the article. Also share it with your friends and colleagues.

Alt Text

Categories
javascript reactjs

useRef Hook | useRef vs useState

⭐ useRef hook

| useRef vs useState

Hey everyone ,

In this article, let us understand the useRef hook in React.js

                    Syntax


       CODE for the video

                  Ref Example – 1

             (An Interval Timer)

  1. As a very first step, import the useState, useRef and useEffect hooks from React.
  2. Setup an initial state for counter and set this to a value of 0 to begin with.
  3. Next let us setup an identifier ref using the useRef hook and set this to an initial value of null to begin with. We are using a ref here instead of a normal variable because we want the identifier ref to persist its value across renders. Also if it changes, it won’t cause a re-render of the component.
  4. Next setup a useEffect hook and here let us setup an interval using the setInterval function and here we want to update the value of counter each time after 1 second.
  5. We can persist the interval value by setting it to the current property of  the identifier ref as shown below. This we will also use in just a second to clear the interval that gets setup when the component unmounts.
  6. So return a function from the useEffect and let’s name it as clearIdentifier.
  7. So define the clearIdentifier function as an arrow function and here we can just call the clearInterval function passing to it our ref value i.e identifier.current Remember the useEffect will only run once after every initial render as we have passed an empty array of dependencies..
  8. In our JSX, let us render the counter value and a button which can be used to clear the interval and stop the counter.

Ref Example – 2 (Working with DOM using refs)

  1. As a very first step, let us import the useEffect and useRef hooks from React.
  2. Setup two refs, one for email and other for password i.e emailRef and passwordRef and set them to an initial value of empty quotes to begin with.
  3. In the JSX that we return, let us setup two input fields inside a form element, one for email and other one for password, and place the created refs on each one of them respectively i.e emailRef on the email field and passwordRef on the password field.
  4. Now on the submission of the form, let us invoke a handleSubmission method. Here we want to tap the values of email and password from the form fields using the refs. So to access the value of an input field using the refs, we can just say :  

    So to access the value for email from the form, we can say emailRef.current.value and to access the value for the password from the form, we can say passwordRef.current.value. So let us just log them to the console.

    Next let us setup a useEffect hook and on the initial render, let us just set focus on the email using the emailRef. So for this, add the following code :

      useState vs useRef

If you prefer video over article, then check this video where I cover about the useRef hook with the exact same examples.

📺 VIDEO

Thanks for reading !

Categories
javascript reactjs

Learn React with one BIG Project [NOTES included] – Demo and Video Link

Hey everyone ,

I created a full fledged video to help others in learning React with ease.

The video includes :

1. Whiteboard Explanations for every concept that we use in the video.