I recently learnt about node.js so thought why not share my learnings?
Here is the basic fundamentals for an absolute beginners of node.js which can be helpful to make a CLI app
1. Command Line Interface (CLI): 🤓
👉If you are writing a program via command line(Black background with text) then it is a CLI(Command Line Interface)
👉 User talk to program via command line is basically CLI
2. Universal rule: 🤓
👉Programming is nothing but input>> processing>> output
.
👉Meaning is every program is divided into three parts.
👉Input
👉Processing
👉Output
3. How to get output or print in node.js ? 🤓
console.log("Hello world");
Output 👇
Hello world
4. What is a String: 🤓
Basically,there are total seven primitive data type:
- String
- Number
- Bigint
- Boolean
- Undefined
- Symbol
- Null
👉So,String is one of them which is basically a texual content enclosed in single/double quotation marks
console.log("**I am Samyak Shah**");
5. Variable: 🤓
Variable: A placeholder for storing the information and whenever required,we can take it from them.
👉For example we assume a study table(placeholder) on which we put pen,pencil,calender,lamp etc. So things are stored in placeholder,whenever required,we will get it from them by defining it.
var myName = "Samyak";
console.log(myName);
Output 👇
Samyak
6. String Concatenation: 🤓
👉String Concatenation: Suppose we have one variable: myName in which "Samyak" is stored. Say We have to welcome Samyak. So, console.log("Welcome " + myName); is string concatenation.
var myName = "Samyak"
console.log("Welcome " + myName)
Output 👇
Welcome Samyak
7. Packages: 🤓
👉Packages: A package is a directory with one or more modules inside of it and a package. json file which has metadata about the package.
👉Basically,In programming,we can use code written by others too...thats what package is.
8. How to take input from User in CLI? 🤓
👉To take input from user,in browser we use prompt() while in CLI,we use ReadlineSync npm library
https://www.npmjs.com/package/readline-sync
var readlineSync = require('readline-sync'); //package
// In repl.it, package is installed automatically
var userName = readlineSync.question("May I have your name? ");
console.log("Hi" + userName + "!");
Output 👇
May I have your name?
// You have to answer it now
Samyak
// final output
Hi Samyak!
9. If else statement: 🤓
👉Branching is usually done with if else. Also note that branching of code, i.e. only one branch runs based on a CONDITION
var readlineSync = require('readline-sync'); //package
var userName = readlineSync.question("Am I older than 25?")
if (userName=== "No"){
console.log("You are right!")
}else {
console.log("You are wrong!")
};
Output 👇
// case 1
Am I older than 25? No
You are right!
// case 2
Am I older than 25? Yes
You are wrong!
10. Function : 🤓
👉Know Syntax:
function functionName(parameterOne, parameterTwo) {
return outputValue;
}
👉Function should be followed by
-The name of the function.
-A list of parameters to the function, enclosed in parentheses and separated by commas.
-Something that defines a function,enclosed in curly brackets.
function **add**(x,y) **{**
//see curly brackets
//note that add is name of function
// x,y are list of parameters to function enclosed in parentheses and seperated by commas.
var sum = x + y; //processing
return sum;
**}**
var output = add(3,4); //calling the function
console.log(output)
👉What is return?
When a return statement is used in a function body, the execution of the function is stopped. If specified, a given value is returned to the function caller.
👉What is calling a function?
When you define a function you give a name to a set of actions you want the computer to perform. When you call a function you are telling the computer to run (or execute) that set of actions.
11. Function Parameters and Arguments: 🤓
👉Know about parameters and arguments: 👉Parameters: function functionName(parameterOne, parameterTwo) Bold texts are Function parameters 👉Arguments: function functionName(5,10) The real values passed to function are Function arguments
function functionName(parameterOne, parameterTwo) //function parameters
function functionName(1,2) //function arguments
12. Understanding for loop: 🤓
calling a function from the loop is running the mini-program multiple times: This is nothing but console.log in for loop to get output
//syntax
for (intial CONDITION; exit CONDITION; change CONDITION) {
}
//program
for (let i = 0; i < 3; i++) {
console.log("Samyak Shah")
}
Output 👇
Samyak Shah
Samyak Shah
Samyak Shah
Note :
👉Initial sets a variable before the loop starts (let i = 0).
👉Exit defines the condition for the loop to run (i must be less than 3).
👉Change increases a value (i++) each time the code block in the loop has been executed.
13. Array: 🤓
👉Data structure is just a way of organizing data in a particular way. In the case of an array, data is arranged sequentially and thus can be accessed using index numbers.
👉Think of the array like contents of a book. You can see what's there in each chapter from the top and directly go to a chapter when you know the page number. All chapters are in a sequence. 👉So exactly like that you can relate to Array
Must know:
👉index-based access, the index starts at 0 not from 1
var listToBuy = ["Pen","Pencil","Eraser"]
14. JavaScript Objects: 🤓
JavaScript Objects:
👉The Object class represents one of JavaScript's data types. It is used to store various keyed collections and more complex entities.
{
key: value;
},
👉 This world is trying to map everyday things to programming concepts.When you think about superman,lets define it: Superman has powers which is to fly,costumecolors which is blue so
power: "Flight"
costumeColors: "Blue"
👉Power and costumeColors are the key and Flight and Blue are the values.
👉Note that different values in datastructures are seprated by comma ( , )
var superman = {
power: "Flight",
costumeColors: "Blue",
}
How to access these values?
console.log(superman.power)
// dot accesses the property of superman
Output 👇
Flight
👉Also note that console
is an object on which log()
is a property. And yes, functions too can be a property of object.
👉When functions are properties of an object, they are called methods
in programming.
I hope so far you have gained the learnings for node.js.
If you liked my content, how about connecting with me?