MODULE - Glowing Neon Sign on stonework wall - 3D rendered royalty free stock illustration.  Can be used for online banner ads and direct mailers.

Creating a Module

In JavaScript, we can use modules to organize our classes so our code is more manageable. We can put as many classes as we want into a module. Each module is its own JavaScript file. Typically, the functionality related to that module is put into the file and organized accordingly…in classes, objects, etc.

Imagine if we have a JS file with the Animal class…

class Animal { 
    constructor(name){
        this.name = name;
    }
    speak(){
         return ${this.name} says;
    }
}

The way it gets used by other JS is by using the “export” keyword.

export class Animal {
     constructor(name){
        this.name  = name;
    }
    speak(){
         return ${this.name} says;
    }
}

Tada! We have created an JavaScript module. The way we use it is by importing it into another file.

Happy Coding!

Clay Hess

Share this post

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on pinterest
Share on print
Share on email
Skip to content