hashnode-blogs

Your first Javascript code in terminal ๐Ÿ˜ƒ

Prerequisites

Lets write your first line of code

Now its time for fancy stuffs like

module.exports & require()

Let me clear you a few things before we go ahead

When you start doing cool stuffs with javascript in terminal instead of browser.

you write javascript code in file like index.js, that file is known as a module.

Now there are two types of modules

by default node supports Commonjs module system

What it feels like writing code in CommonJS module system

you can communicate between two files ( modules ) and use one another codes, functions, classes

Lets do some experiments ๐Ÿงช

Experiment #1

Experiment #2

Experiment #3

from this experiment, what I conclude is



module.exports = ["an", "array", "with", 1, "boolean", "value", true];

now milkyWaySomeWhereElse will be equal to ["an", "array", "with", 1, "boolean", "value", true]


module.exports = {
sky:"it is blue",
hello:"you can export a object"
};

In this case milkyWaySomeWhereElse will be equal to
{ sky:"it is blue", hello:"you can export a object" }


module.exports = function(){
return "you can a export a function which returns some value";
}
// or
// module.exports = function(){
//  console.log("you can a export a function which do not returns any value");
// }

milkyWaySomeWhereElse is a function now
the function is declared only and get assigned to another variable,
but not called yet!
or not executed yet
This function is same as normal functions in javascript,
to call or execute write </br> milkyWaySomeWhereElse()
console.log(milkyWaySomeWhereElse()) will output the string
"you can a export a function which returns some value"



Its your turn ๐ŸŽฏ

I have written a coding-challenge based on this topic.

NOTE: I will write about ECMAScript (ES6) modules in next post.