NODE JS: Beginning #1
Here are the details for the code we used:
var means: Any Variable
Console.Log : print something
# 4 types of Compilers:
⦁ Online Compiler
⦁ Vs Code
⦁ Browser Run
⦁ Terminal
Here is the simple program to run in java:
Var a = 5;
Var b = 5;
Var ans = a*b;
Console.log(ans);
Output:
25
-----------------------------------------------------
#Basics Of JS Will Be Using In Node JS:
Variable
Objects
JSON
Console
Array
Random
If
Else
Loop
Functions
Prompt
___________________________________________________
We can use "let" instead of using "var" keyword to define variables
Keyword : Const
-> A const keyword is used to create datatypes constant. i.e.(That Cannot Be Changed)!
syntax:
const name = 'Ram';
and if we change it to Shyam
like:
name = 'Shyam'
Error: It gives an error that constant variable cannpt be changed;
If you want to know what types of data is stored in variable you can see .
Let's see example for that:
console.log ( typeof name );
___________________________________________________
Object Datatypes:
-> used to create multiple types of datatypes that can be string, integer, etc.
const cars = ["Tesla", "Volvo", "Lamborgini"];
cars.push("Rolls Royace");
console.log(cars);
conslole.log(cars[2]); //used to retrieve data from that index
console.log(cars[0]);
Output :
string
Lamborgini
Tesla
___________________________________________________
If - Else Statement:
-> used for checking whether it is true or not according to condition applied on it.
for ex:
var a = 10;
if(a < 20){
console.log("You are permitted To Class");
}
else{
console.log("You are Not Permitted To Class");
}
___________________________________________________
Loop Statement:
-> Loop is an statement that uses for doing repeatedly process in it until the condition is true.
for ex :
if we have to print large number of table let suppose to print 10 number
its easy
but if it is asked to print 100 you say its easy to print line by line one code or copying one just change their values.
but what happen when it is asked to print 1Lakhs data. You can't do it by writing one by one code.
Here comes the concept of looping that do the task repeatedly until the condition is true.
Syntax:
let a = 10;
for(let i = 1; i < a; I++){
console.log(i);
}
Output :
1 2 3 4 5 6 7 8 9
___________________________________________________
OBJECT :
-> Object is a real world entity that can be place, person, name etc.
const person = {
name : "Ram",
Teacher : false,
age : 39,
Address : "Vadodara, Gujarat, India",
Hobbies : ("Cricket", "FootBall", "Coding")
};
console.log(person);
console.log(person.Teacher);
Output:
{
name: 'Ram',
Teacher: false,
age: 39,
Address: 'Vadodara, Gujarat, India',
Hobbies: 'Coding'
}
false
___________________________________________________
Here, we create a function to filter from given arrays:
let us having arrays Like:
ages : [32,4,56,18,89];
we have ages like under 18 : 4 only ;
and above 18+ : 32, 56, 89
here we use filter function to filter that arrays:
Example:
const ages : [32,4,56,18,89];
const result = ages.filter(CheckAge);
function checkAge(age){
return age >= 18;
}
console.log("Above 18+ : "", result);
Output: Above 18+ : [ 32, 56, 18, 89 ]
// return age >= 18;
console.log("Below 18+ : "", result);
___________________________________________________
User Input :
-> let suppose we have to take input from user then we use prompt keyword to get input from users
to use Prompt Keyword we have to install npm in our system to support or use the prompt keyword .
Here is the npm for prompt:
npm i prompt-sync
#Here is the code for getting input from user :
var prompt = require('prompt-sync')();
// to get Input From User:
const age = prompt("Enter Age: ");
if( age < 18){
console.log("Get 20% Discount");
}
else{
console.log("30% Discount");
}
Comments
Post a Comment