3226. Number of Bit Changes to Make Two Integers Equal using JavaScript

0 Problem You are given two positive integers n and k. You can choose any bit in the binary representation of n that is equal to 1 and change it to 0. Return the number of changes needed to make n equal to k. If it is impossible, return -1. Example 1: Input: n = 13, k = 4 Output: 2 Explanation:Initially, the binary representations of n and k are n = (1101)2 and k = (0100)2.We can … Read more

How to start content writing

Freelancing diary placeholder featured image

0 Start content writing, where you can share your thoughts and stories with the world. Blogging lets you express yourself and can even help you make money or grow a business. This guide will show you how to start writing for blogs, step by step, in a way thatโ€™s easy to understand. Step 1: Identifying … Read more

Guide on how to starting blogging: Tips, Tricks, and Strategies

Freelancing diary placeholder featured image

0 Embarking on Your Blogging Adventure Starting a blog can be a thrilling and rewarding endeavor. Learn how to starting blogging and start sharing your passions. Blogging is all about connecting with like-minded individuals, and even earning an income. In this guide, weโ€™ll explore the key steps to creating a blog that resonates with readers … Read more

How to start YouTube channel

0 Introductionย Are you ready to share your passion with the world through video? Starting a YouTube channel is a fantastic way to do just that. Whether youโ€™re aiming to educate, entertain, or inspire, this guide will walk you through the essential steps to launch your channel successfully. Understanding the Platform Before diving in, itโ€™s crucial to … Read more

Understanding ES6 Modules: A Beginnerโ€™s Guide to JavaScriptโ€™s Powerful Feature

Freelancing diary placeholder featured image
0

Demystifying ES6 Modules: A Practical Walkthrough

JavaScript has evolved significantly over the years, and one of its most powerful advancements in recent times is the introduction of ES6 modules. These modules bring a new level of clarity and structure to JavaScript codebases, making it easier to organize, maintain, and share code among projects.

If you want to skip reading and would like a youtube video for this topic?

What Are ES6 Modules?

ES6 modules are a way to encapsulate code into small, reusable pieces. They allow developers to export parts of a module (like classes, functions, or variables) and import them in other modules, promoting a cleaner and more modular code structure.

A Look at the Code

Letโ€™s dive into an example to see ES6 modules in action:

File: main.js

JavaScript

import User, { printAge, printName } from "./new.js";

let sahil = new User("Sahil Ahlawat", 10);
printAge(sahil);
printName(sahil);

File: new.js

JavaScript

export default class User {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
}

export function printAge(user) {
    console.log(`Age of user is : ${user.age}`);
}

export function printName(user) {
    console.log(`Name of user is : ${user.name}`);
}

In new.js, we define a User class and two functions, printAge and printName. We then export these so they can be used in other files. In main.js, we import these exports and use them to create a new User object and print its details.

Setting Up Your package.json

To ensure Node.js treats our .js files as ES6 modules, we need to add a "type": "module" line to our package.json:

File: package.json

JSON

{
  "name": "modules",
  "version": "1.0.0",
  "description": "Modules tutorial",
  "type": "module",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Sahil Ahlawat",
  "license": "ISC"
}

With this setup, running node main.js will execute our code with ES6 module support.

Benefits of Using ES6 Modules

  • Reusability:ย Code can be shared across different parts of an application or even different projects.
  • Maintainability:ย Clearer project structure makes it easier to manage and update code.
  • Namespace:ย Avoid global namespace pollution, which can lead to fewer bugs.

Conclusion

ES6 modules are a significant step forward in JavaScript development. They offer a robust way to organize code, making it more readable and maintainable. By embracing this feature, developers can improve their workflow and create more scalable applications.


This blog post provides a clear explanation of ES6 modules, demonstrates their usage with your code, and explains how to set up a Node.js project to use them. Itโ€™s written in an accessible way that should appeal to both beginners and experienced developers alike.

121. Best Time to Buy and Sell Stock using JavaScript

0 Problem You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. Example … Read more

42. Trapping Rain Water using JavaScript

0 Problem Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining. Example 1: Input: height = [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are … Read more

Top 10 best laptops for software development under 1 lakh in 2024 | Freelancing gadgets

0 Are you worried about finding the best laptop for your needs and budget? Donโ€™t worry! We have selected the best laptops for you that cost less than 1 lakh rupees. Whether you are a coder looking for the best laptops for coding, a freelancer in search of the best laptop for freelancing, or a … Read more

167. Two Sum II – Input Array Is Sorted using JavaScript

0 Problem Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length. Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2. The tests are generated such that there is exactly … Read more

125. Valid Palindrome using JavaScript

0 Problem A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string s, return true if it is a palindrome, or false otherwise. Example 1:Input: s = “A man, a plan, a canal: Panama” Output: true Explanation: “amanaplanacanalpanama” … Read more

Skip to content