JS
JS
var x = 5;
var y = 5;
//This will print false because,this is not the correct way
// console.log("Is x and y are equal or not"+ x==y);
//*****************************************************************//
//What will be the output of 3**3
// console.log(3**3); This(**)means 3 ki power 3
/********************************************************/
// Q1 What is the difference btw == and ===?
// first we are using ==
var num1 = 5;
var num2 = '5';
// console.log(typeof(num1));
// console.log(typeof(num2));
// console.log(num1 == num2);
// so the key difference here is that the == checks only the value
// but the === checks the datatype as well and returns the value then
/*************************************************************************/
// Program to print table of given number
// for(var number=1;number<=10;number++){
// var tableOf=8;
// console.log(tableOf +" * "+number + " = "+tableOf * number);
// }
/***********************************************************************/
// Function expression
// function sum(a,b){
// return a+b;
// }
// var funexp=sum(16,6);
// console.log(funexp);
/*******************************************************************************/
// Anonymous Function:- are the function jinka koi naam na ho
// var funexp=function(a,b){
// return a+b;
// }
// console.log(funexp(12,2));
/******************************************************************************/
// Fat Arrow Function
// The difference btw normal function and fat arrow function is that we can call
function
//before declaration in normal function ,but in fat arrow function we have to
//declare first and then call it.and the difference of syntax also
/*****************************************************************************/
// For in and For of loop in EcmaScript 6
//forin:- It returns the index no. of elements present in array
// var myFriends=["ravi","kuldeep","mohit","nikhil"];
// for(let elements in myFriends){
// console.log(elements);
// }
/***************************************************************************/
//Array.prototype.forEach()
// var myFriends=["ravi","kuldeep","mohit","nikhil"];
// myFriends.forEach(function(element,index,array){
// console.log(element);
// });
//Same forEach by fat arrow function
// var myFriends=["ravi","kuldeep","mohit","nikhil"];
// myFriends.forEach((element,index,array)=>{
// console.log(element);
// });
/**************************************************************************/
// There are four methods in which we Insert,Add,Delete elements from an array
// 1.Push-Used to add elements at the end of an array and returns the new length of
array
// 2.Unshift=Used to add elements at the Beginning of an array and returns the new
length of array
// 3.Pop=Used to delete elements at the end of an array and returns the new length
of array
// 4.Shift=Used to delete elements at the Beggining of an array and returns the new
length of array
let myarr=[1,2,3,4,8,6]
// myarr.push(5)
// myarr.unshift(12)
// myarr.pop()
// myarr.shift()
// console.log(myarr)
// console.log(myarr.length)
/*****************************************************************************/
//Splice Method
//Adds or remove elements from an array
//1.Add dec at the end of an array
//2.What is the return value of splice method
//3.update march to March
//4.Delete june from an array
//const months=['Jan','march','April','June','July']
// sol1
// const newMonth=months.splice(5,0,'dec');//Here we pass three parameters
// console.log(months); //(index at which we want to add the element,what we want
to delete ,element name tht we want to add)
//sol2
// console.log(newMonth);
// returns the element which we delete ,but in this case returning a empty array []
// sol3
// const months=['Jan','march','April','June','July']
// const newMonth=months.splice(1,1,'March');
// console.log(months);
//sol4
// const months=['Jan','march','April','June','July']
// const newMonth=months.splice(3,1);
// console.log(months);
/************************************************************************/
// Map Method:-It returns the value in boolean(true or false)
// const array1=[1,3,9,16,25];
// // num>9
// const newarray=array1.map((curelm,index,array1)=>{
// return curelm>9;
// });
// console.log(array1);
// console.log(newarray);
/
***********************************************************************************
******/
// Q1 Find the square root of each element in an array?
// let arr=[25,36,49,64,81];
// let arrSqr=arr.map((curElm)=>{
// return Math.sqrt(curElm)
// });
// console.log(arrSqr);
// Q2 Multiply each element by 2 in the array and return only those whose value is
greater than 10?
// let arr=[2,3,4,6,8]
// let arr2=arr.map((curelm)=>{
// return (curelm*2);
// }).filter((curelm)=>{
// return curelm>10;
// })
// console.log(arr2);
//REDUCE METHOD:- Used to flatten 3d or 2d array and also used for calculating
//sum,av,mul,div of elements of an array,which we cant do with map or filter
// Takes four arguement(Accumulator,curelm,index,arr)i.e Meaning of accumulator
//is jama karna ek jagah pr
// example
// let arr=[5,6,2];
// let sum=arr.reduce((Accumulator,curelm,index,arr)=>{
// return Accumulator *= curelm;
// });
// console.log(sum);
//can add this reduce method in above solved question too with map and filter
method
/*********************************************************************************/
//STRINGS
//Escape Character:-Used when string ke andar string use krna chahte ho toh
// let str="Hey Fellows I am \"Nikhil\" from Jaipur";
// console.log(str);
//Or Use the Alternate quotes (agar main string mai double quotes use kr re ho toh
andr wali mai single use krlo and vise versa)
// let str="Hey Fellows I am 'Nikhil' from Jaipur";
// console.log(str)
//Extracting a String
// 1.Slice Method
// let str="Apple, Bananas, kiwi";
// let result=str.slice(0,4);//Output is Appl :- Last index ka character include
nhi hota
// console.log(result);
// let res=str.slice(7,-2);
// console.log(res);//End tk jaega 7 se or akhiri se -2 charcter kar dega
// 2.Substring() Method
//This method is same as the slice method but it does not accept negative indexes
//Agar hum negative index denge to ye shuru se 0th position se index count krkr
print krdega
// example
// let str="Apple, Bananas, kiwi";
// let result=str.substring(0,4);//Output is Appl :- Last index ka character
include nhi hota
// console.log(result);
// let res=str.substring(8,-2);
// console.log(res);
/*********************************************************************************/
//Extracting String Characters
// 1.charat():-Returns the position of the character
// let str="Hello World";
// console.log(str.charAt(0));
/*******************************************************************************/
// Other Method
// 1.Split Method:-String can be converted to an array
// let str="a,b,c,d,e";
// console.log(str.split(",")); //split on basis of comas
// console.log(str.split(" ")); //Basis of space
/***************************************************************************/
//DATE AND TIME METHOD
// let currDate = new Date();
// console.log(currDate);
// console.log(currDate.toLocaleString());
// console.log(currDate.toString());
/**********************************************************************/
//Math Objects
// 1.Math.PI
// example:-
// console.log(Math.PI);
// 3.Math.pow
// example
// console.log(Math.pow(2,3));
// 4.Math.sqrt
// example
// console.log(Math.sqrt(25));
// 5.Math.abs
// example
// console.log(Math.abs(-66));
// 6.Math.ceil
// example
// 7.Math.floor
// example
// console.log(Math.floor(99.7));//Ceil ka ulta decreament karega
/*************************************************************************/
// OOPs Javascript
//1 st way
// let biodata={
// myname:"nikhil",
// myage:26,
// getData:function(){
// console.log(`Name is ${biodata.myname} and age is $
{biodata.myage}`);
// }
// }
// biodata.getData();
//The "this" object can have can have diff values depending on where it is placed
// example1
// console.log(this); [If wants to check then run on console]
//It refers to the current context and that is window global object
// example2
// function myname(){
// console.log(this)
// }
// myname(); [If wants to check then run on console]
// It refers to the current context and that is window global object
// example3
// let mynames="niksss";
// function myname(){
// console.log(this.mynames)
// }
// myname();
// }
// obj.myname();
// example5
// this object will not work with arrow function
// const obj= {
// myage : 18,
// myname:()=> {
// console.log(this);
// }
// }
// obj.myname();
/*********************************************************************************/
// Destructuring in ES6
// the destructuring assignment syntax is a js expression that makes it possible to
// unpack values from arrays,or properties from objects,into distinct variables
//=>Array Destructuring
// const myBiodata=['vinod','thapa',26];
// let myFname=myBiodata[0];
// let myLname=myBiodata[1];
// let myAge=myBiodata[2];
// console.log(myAge);
//Object Properties
//>we can now use dynamic properties
// let myname="nikhil";
// const mybio={
// [myname]:"hello,how r you",
// [20+6]:"is my age"
// }
// console.log(mybio);
/***************************************************************************/
//Spread Operator
// let colour=['red','green','pink','orange'];
// let mycolour=['yellow','black'];
// let myfavcolours=['yellow','black',...colour];
// console.log(myfavcolours);
/************************************************************/
// ES7 features
// 1.Array include
// let colour=['red','green','pink','orange'];
// const isPresent=colour.includes("red");
// console.log(isPresent);
// 2.Exponential(**)
// console.log(2**2);
/**********************************************************/
// ES8 features
//String Padding
//Object.values()
//Object.entries
// 1.String padding
// we have two methods padstart and padend
// used to add padding in the elements at starting or ending
// let myname="nikhil".padStart(18);
// console.log(myname);
// let myage="26".padEnd(12);
// console.log(myage);
//ES2019
/********************************************************************/
//Flat method to flatten the array
// let arr=[
// ['zone1','zone2'],
// ['zone3','zone4'],
// ['zone5','zone6'],
// ['zone7',['zone7','zone8']]
// ];
// console.log(arr.flat(2));
// const arrobj=(Object.entries(person));
// console.log(Object.fromEntries(arrobj));
/****************************************************************/
//ES2020
// //Bigint
// let oldnum=Number.MAX_SAFE_INTEGER;
// console.log(oldnum);
//if we want calculation of number bigger than this then simply add n
//Nullish coalescing
// The nullish coalescing operator (??) is a logical operator
// that returns its right-hand side operand when its left-hand side
// operand is null or undefined, and otherwise returns its left-hand side operand.
// example
// const foo = null ?? 'default string';
// console.log(foo);
// // expected output: "default string"
/************************************************************************/
//2.Callback function
//function which gets passed as an arguement to another function called cbf
// example of both
//we need to create a calculator
// const add=(a,b)=>{
// return a+b;
// // like we do
// // console.log(add(5,2));
// }
// const subs=(a,b)=>{
// return Math.abs(a-b);
// }
// const mul=(a,b)=>{
// return a*b;
// }
// const div=(a,b)=>{
// return a/b;
// }
// const calculator=(num1,num2,operator)=>{
// return operator(num1,num2);
// }
// console.log(calculator(5,2,add));
//Hoisting in javascript
// it is a mechanism in where variables and fucntions declarations are
// moved to the top of their scope before the code execution
// example
// console.log(myname);
// var myname;
// myname="nikhil";
// this will returns undefined
// At the top of the scope chain is the global scope, which is the
// window object in the browser
// const first=()=>{
// let b="How are you?"
// const second=()=>{
// let c="Hi,Am fine";
// console.log(a+b+c);
// }
// second();
// // console.log(a+b+c);//I cant use C
// }
// first();
/
********************************Restart********************************************
/
// let obj1={
// greeting:"Good Morning",
// names:["Harry","Nikhil","Ravi"],
// speak(){
// this.names.forEach((student)=>{
// console.log(this.greeting +" Hello "+student);
// });
// }
// }
// obj1.speak();