Posts

Showing posts from December, 2023

Understanding Classes, Objects, Prototype, Inheritance, Constructors, super(), and this in JavaScript

JavaScript, is an object-oriented language, with various principles such as classes, inheritance, constructors, super(), and this facilitates the creation and organization of code. Let's dive into these concepts and understand their significance through simple examples. Classes in JavaScript JavaScript introduced the concept of classes in ECMAScript 2015 (ES6), providing a more structured way to create objects. A class is a blueprint for creating objects with shared properties and methods. Example: Creating a Simple JavaScript Class Let's consider a Vehicle class: class Vehicle {   constructor(make, model) {     this.make = make;     this.model = model;   }   displayInfo() {     console.log(`Make: ${this.make}, Model: ${this.model}`);   } } In this example, a Vehicle is a class with a constructor that initializes its make and model properties. It also contains a displayInfo() method to show the vehicle's information. Objects in JavaScript Let's use our classes to create

Loops in JavaScript - Exploring Their Differences and Best Use Cases

In JavaScript, there are various loop structures, each with its unique characteristics and best use cases. In this blog post, we'll delve into some of the most commonly used loops: (for-of vs for-in), (for loop vs while loop), (while loop vs do-while loop), and more. Let's explore their differences, advantages, and when to use each type. 1. for-of vs for-in Loop for-of Loop The for-of loop is used for iterating over iterable objects like arrays, strings, maps, and sets, focusing on their values. Example: const myArray = [1, 2, 3, 4, 5]; for (const element of myArray) {     console.log(element); } for-in Loop The for-in loop is Used for iterating over object properties (keys), including inherited ones, focusing on keys rather than values. Example: const myObject = { a: 1, b: 2, c: 3 }; for (const key in myObject) {     console.log(key, myObject[key]); } 2. for Loop vs while Loop for Loop The for loop is versatile and commonly used when you know the number of iterations. It consi

Map, Filter, and Reduce Methods in Javascript

   JavaScript provides powerful array methods like map, filter, and reduce, enabling developers to manipulate and transform arrays efficiently. In this blog post, we will use practical examples to showcase these methods' versatility and utility. Map Method: Transforming Data  The map method transforms each element of an array and creates a new array with the transformed values. Let's take a look at an example : const arr = [5, 1, 3, 2, 6] //convert to binary = [101, 1, 11, 10, 110 ] const convertBinary = function(x){    return x.toString(2) } const output = arr.map(convertBinary)                     Or const output = arr.map((x) => x.toString(2))     The Filter Method - The filter method is used to create a new array containing elements that pass a certain condition or criteria.   Let's take a look at an example : const arr = [5, 1, 3, 2, 6] //filter out all odd number = [5,1,3] const allOdd = function(x){ return x%2 } const output = arr.filter(allOdd)                 Or