0% found this document useful (0 votes)
12 views15 pages

JS

Uploaded by

lakshya.s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views15 pages

JS

Uploaded by

lakshya.s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 15

//Assignment Operator

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);

//Correct Way is here


// 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);

//now we are using ===


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

//Normal Way of Writing Function


// console.log(sum());
// function sum() {
// let a = 2, b = 3;
// let sum = a + b;
// return (`The sum of a+b is ${sum}`);
// }

//Convert it into fat arrow function


// const sum=()=>{
// let a = 2, b = 3;
// let sum = a + b;
// return (`The sum of a+b is ${sum}`);
// }
// console.log(sum());

// 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);
// }

//Forof:- It returns the elements present in array


// var myFriends=["ravi","kuldeep","mohit","nikhil"];
// for(let elements of 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);

//Filter Method:-It return the actual value


// const array1=[1,3,9,16,25];
// // num>9
// const newarray=array1.filter((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?

// WE ARE USING CHAINING OF TWO METHODS HERE

// 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));

// 2.charCodeat():-Returns the UTF-16 code of the character


// let str="Hello World";
// console.log(str.charCodeAt(1));

//3.Property Access:-Allows property access[ ] on strings


// let str="Hello World"
// console.log(str[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());

// console.log(Date.now());//returns the time in milisecond since jan 1 1970

//After this we have done index1.html

/**********************************************************************/
//Math Objects
// 1.Math.PI
// example:-
// console.log(Math.PI);

// 2.Math.round:-round offf to the nearest integer


// example
// let n=10.449999;
// console.log(Math.round(n));

// 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

// console.log(Math.ceil(99.1))//Increament krega hi krega round off ki tarah nhi


hai

// 7.Math.floor
// example
// console.log(Math.floor(99.7));//Ceil ka ulta decreament karega

// 8.Math.trunc:- just returns the integer part of a number


// example
// console.log(Math.trunc(-99.7)); returns -99

/*************************************************************************/
// OOPs Javascript

// Object Literals:-is simply a key:value pair data structure


// storing variables and functions together in one container,refer as object

//1 st way
// let biodata={
// myname:"nikhil",
// myage:26,
// getData:function(){
// console.log(`Name is ${biodata.myname} and age is $
{biodata.myage}`);
// }
// }
// biodata.getData();

//2nd way :-No need to write function


// let biodata={
// myname:"nikhil",
// myage:26,
// getData(){
// console.log(`Name is ${biodata.myname} and age is $
{biodata.myage}`);
// }
// }
// biodata.getData();

//What if we want object as a value inside an object


// let biodata={
// myname:{
// realname:"nikhil",
// nickname:"Virat"
// },
// myage:26,
// getData(){
// console.log(`Name is ${biodata.myname} and age is $
{biodata.myage}`);
// }
// }
// console.log(biodata.myname.nickname);
// biodata.getData();

//What is (this) object?


//The definition of "this" object is that it contains the current context

//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();

// example 4 :-using object


// const obj= {
// myage : 18,
// myname() {
// console.log(this.myage);
// }

// }
// obj.myname();

// example5
// this object will not work with arrow function
// const obj= {
// myage : 18,
// myname:()=> {
// console.log(this);
// }

// }
// obj.myname();

// It refers to the current context and that is window global object

/*********************************************************************************/
// 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);

//Here come role of Destructuring


// let [myFname, myLname,myAge]=myBiodata;
// console.log(myFname);

//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);

//No need to write key and value if both are same


// example
// let myname="nikhil"
// let myage=18;
// const mybio={
// myname:myname,
// myage:myage
// }
// console.log(mybio);

// Above thing should be written like this :-


// let myname="nikhil"
// let myage=18;
// const mybio={
// myname,
// myage
// }
// 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);

/************************Object values and entries **********************/


// const person={
// name:'Fred',
// age:26
// }
// console.log(Object.values(person));
// console.log(Object.entries(person));

//ES2019
/********************************************************************/
//Flat method to flatten the array
// let arr=[
// ['zone1','zone2'],
// ['zone3','zone4'],
// ['zone5','zone6'],
// ['zone7',['zone7','zone8']]
// ];
// console.log(arr.flat(2));

//This simply only flat() cant flat array inside array


//we have to pass the parameter as levels in flat method
//if we want to flat array inside array inside array so we have to pass
//3 as level like:>flat(3);
//or simply pass infinity if dont have idea about levels
//Object entries convert object into array
//Now we have method object fromentries which convert array into object
// example
// const person={
// name:'Fred',
// age:26
// }

// 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

// const newnum=9007199254740991n + 12n;


// console.log(newnum);
// console.log(typeof(newnum));

//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"

// const baz = 0 ?? 42;


// console.log(baz);
// // expected output: 0

/************************************************************************/

//1.Higher Order Function:-


// function which takes another function as an arguement is called HOF

//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));

//Here calculator is higher order function and add,sub,etc.are callback functions

//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

//How it will be in output during creation phase


// var myname;
// console.log(myname);
// myname="nikhil";
/*************************************************************/
// Scope chain and lexical flexing in javascript

// The scope chain is used to resolve the value of variable names in


// javascript.Scope chain in
// javascript is lexically defined, which means that we can see what the
// scope chain will be by looking at the code.

// At the top of the scope chain is the global scope, which is the
// window object in the browser

// Lexical scope means that in a nested group of functions, the inner


// functions have access to the variables and other resources of their
// parent scope but vice versa is not true
//For example

let a="hello guys."//global scope

// 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();

You might also like