You are on page 1of 11

Bascis of JS

Word vs Keyword:
Word are the word which has no meaning in JS

Keyword are the word which has meaning in JS like IF else

Variable Constant Let:


Variable:
We Use to create Variable by writing like var ali=1;
We can change variable during execution

Let:
We will explain in Advance Lecture because it is a lecture of basic js

Constant:
We Use to create Constant by writing like const ali=1;
We cannot change Constant during execution

Hoisting:
Using of variable before decleration
Like
Console.log(a)
Var a =10;
Its answer will be undefined but none not defined

Undefined vs Not Defined:


Undefined:
Value Exist krti hai lekin value Kitni hai yai nhi pata

Not Defined:
Value hi exist nhi krti

Types in JS:
Primitives:
If value will be without brackets then value is Primitives
Example:
var a=10;
var a=b;

Refrence:
If value is between [],{},() then it is refrence
Var a =[10,20,30];

Copy Refrence Value:


We copy data through refrence like b=a kr k jb pop krain gy tou a sy bhi pop hoo ga
Var b=a;
b.pop():it will remove 30 from both a & b
*pop() use to remove last value from bracket*
*aisi koi bhi value jis ko copy karne par real copy nhi hota ,balki us main value ka reference pass hojaata
hai,use hum refrence value kehtay hain,aur jis ka copy krny par real copy ho jaaye wo value primitive type
value hoti hai*
Yahan refrence tha & Copy refrence detail JS LWM 2 main hai

Conditionals:
If else else if
If(){
}
Else if{
}
Else if{
}
Else{
}

Loops:
Loop mean repeat
For Loop:
For(start;end;change){
}
For(var i=10;i<51;i++){
Consol.log(i);
}
While:
var i=1;
while(i<20){
document.write(i);
i++;
}
Functions:
1) Jab ap code usi waqat nhi chalana chatay hoo bahad main chalana hoo
2) For Code Reuse
3) To Ascces Code with different code
function abcd(){
document.write("Ali")
}
abcd();
abcd();
Arguments & Parameters:
To Ascces Code with different code
function abcd(a,b,c)
//This is Parameter where we store value through arguments
{
document.write(a,b,c);
}
abcd(10,20,30);
//This is argument through which we tranfser value to parameter
Arrays:
Use to store more than one value in one variable
var a=12,13; This is wrong
var a=[12,13];

index:
index is a counting of array cells like 0-So on Address of array data
first value store in 0
seconed in 1

Third in 2

Push,Pop,Shift,Unshift:
Push:
Use to add extra member in last
Var a=[1,2,3,4,5,6,7];
a.push(8);
Result:
1,2,3,4,5,6,7,8

POP:
Use to remove last member from last
Var a=[1,2,3,4,5,6,7];
a.pop();
Result:
1,2,3,4,5,6

Shift:
Use to remove first value
Var a=[1,2,3,4,5,6,7];
a.shift();
Result:
2,3,4,5,6,7

Unshift:
Use to add a value in the start of array
Var a=[1,2,3,4,5,6,7]
a.unshift(0)
Result:
0,1,2,3,4,5,6,7

Splice:
Use to remove value b/w array like:
Var a=[1,2,3,4,5,6,7]
a.splice(2,1)
Result:
1,2,4,5,6,7
It remove index 2
And 1 means it remove only 1 value if we write 3 then it remove 3 value from index like remove 3,4,5 and its
resulte will be 1,2,6,7

Objects:
Hold information about only one indivisual

Blank Obj:
Var a={}

Filled Obj:
Var a={
Name:”Ali”,
Age=18,
CNIC=38404-1892160-1
}
Access of Obj
a.Name

Prop VS Method:
Upper Program is example of Prop & if we write a function in front of Property then we say it a Method Like
Var a={
Name : ”Ali”,
Age:18,
CNIC:38404-1892160-1
Class:function(){}
}
Updating Obj Properties:
Changing value of any property
Like in upper function => a.Name=”Sultan”; Now it print Sultan
Advance JS
JS Two Version:
Es5:
Old version than Es6 here was only var

Es6:
Latest version then Es5. Here is var , let & const.

Var Let & Const:


Var:
1)Var function scoped hota hai.
2)Var apny parent function main kahin bhi use hoo skta hai

function abcd(){
for(var a=1;a<11;a++)
{
console.log(a);
}
console.log(a);
}
abcd();
yahan pr andar & Bahir wala console.log douno hi chalain gy q k y yai function main variable hai & 12 tk print hoo ga
3) Var adds to window itself
var=12; var main di gai value window main add nhi hoo gi console main window likanay k bahad doucument pr click sy
sara check hoo skta hai.

Let:
1)Let , const Braces scoped hota hai.
2)Let , const braces scope hai

function abcd(){
for(let a=1;a<11;a++){
console.log(a);
}
console.log(a);
}
abcd();
is function main 2nd console.log nhi chalay ga q k yahan let hai & braces k andar wala let bahir nhi chalay ga jb k var
chalta hai & 10 tk Print hoo ga & 2nd I ka not defined ka eror aay ga.
3)Let & Const dose not adds to window itself
Let=12;
Let/const main di gai value window main add nhi hoo gi console main window likanay k bahad doucument pr click sy
sara check hoo skta hai.
*Crtl + Shift +J is a shortcut key to open console in window *

Browser context api:


Browser provide us 3 features:
 Window Objects
 Stack
 Heap memory

Window Objects:
JS Main kuch cheezain nhi hain lekin js wo window sy leta hai for example mai nai pen ko kuch kam kahay jo
pen kar sakta hai kray ga & Pen k pass ek box hai jo kam pen nhi kr skta wo kam us box sy ly kr kry ga Pen is
JS and Box is Window and this box is given by browser JS main kuch feature nhi hain lekin wo feature in box
alert,prompt bhi js k part nhi hain konsay feature js main nhi hain console main window likhnay sy check kr
sktay hain.

Stack:
Kon si cheez kesy chlay gi
Sequence of running data

Stack by Bing ask: JavaScript mein stack ek jagah hai jahan data store kiya jaata hai . Stack, ek data
structure hai jahan addition aur removal of elements ek particular order mein hota hai, jo ki LIFO
(Last In First Out) ya FILO (First In Last Out) hota hai . Jab koi function call hota hai, to JavaScript
engine call stack mein uski information store kar deta hai . Call stack mein wo function jo currently
run ho raha hai aur jo functions us function ke andar se invoke kiye gaye hain, unki information
hoti hai .Jab koi function call hota hai, to JavaScript engine uski information ko call stack mein
push kar deta hai. Jab function execute ho jaata hai, to uski information call stack se pop ho jaati
hai . Ye process continue hota rehta hai jab tak ki call stack khali na ho jaaye .
Heap Memory:
Jo bhi data program main use hota hai wo kahi na kahin store hota hai wo data heap memory main store hota hai
Like 1+2+3+4+5 comp pehly 1+2=3 kry ga phir 3+3=6 hoo ga & yai essy calculation hoo gi is process main
temporary value heap memory main store hoo gi

Heap by Bing ask:JavaScript mein heap memory ek jagah hai jahan objects aur functions store kiye
jaate hain . Jab values initially declared hote hain, to JavaScript unke liye memory automatically
allocate kar deta hai . Heap stack se different hai, jo dusra jagah hai JavaScript mein data store
karne ke liye . Engine objects ke liye heap mein fixed amount of memory allocate nahi karta.
Instead, more space is allocated as needed
Execution Context:
Execution context matlab jb bhi hum function chalain gy fnc khud ka ek imaginary container bana ly ga isi
conatinar ko execution contex kehty hain is main three chezain hoti hai
Execution context is a container where the function’s code is excecuted and its created whenever a function is
called it contain three thing which are given below.
Var function(){ }
 Variable
 Functions inside the parent fnc Lexical Environment
 Lexical environment of container
Abcd() When we call Abcd() imaginary container will be create which is EC:

Lexical environment:
Ek chart jis main likha hota hai k ap ka particular function kin cheezon ko acces kr skta hai aur kin ko nhi
matlab k it hold its chain and chain scope
Lexical enviorment by bing ask: A lexical environment is a concept in JavaScript that describes how variables
and functions are stored and accessed in different parts of the code. It consists of two parts: an environment
record and a reference to the outer environment. The environment record is an object that contains the names
and values of the variables and functions declared in the current scope. The reference to the outer environment
is a link to the parent lexical environment, which allows the code to access variables and functions defined in
higher scopes. A new lexical environment is created every time a function or a block of code is executed. The
lexical environment determines the scope of variables and functions, which means where they are visible and
usable in the code.

How to copy Refrence value:


By using spread oparetar LWM 1 main a & b ki value same thi pop() sy a&b douno sy value remove hooti thi
liken copy refrence sy agar a ki krn gy tou a ki hoo gi aur agar b ki krn gy tou b ki hoo gi
Var a=[1,2,3,4,5];
Var b=[…a];
Ab pop a sy sirf a sy value & pop b sy sirf b main sy value remove hoo gi
Obj main bhi hum refrence copy kr skty hain
var obj={
name:"Ali"
}
var objcopy=obj;
idhar agar obj main ya objcopy main jo bhi change kro gy douno main change hoo jay ga
var obj={
name:"Ali"
}
var objcopy={...obj};
idhar wahi change hoo ga jisy krna ho obj main change krny sy objcopy change nhi hoo ga

Conditionals:
Basic LWM 1 main hoo gaya tha

Truthy:
All values will be truthy expect falsy

Falsy:
0, false , undefined ,Null ,NaN , doucument.all
Agr if else main condition ki jagha falsy main sy koi value aay gi phir else statement chlay gi nhi tou if wali
If(-1){
Doucumnet.write(Hello);
}
Else{
Doucumnet.write(Hey);
} its result will be hello bcz -1 is not falsy
Switch Case:
Switch case bahot kam use hota hai

ForEach:
Only apply on array: array by default kabhi bhi change nhi hota forEach sy forEach temporary image main bus
array ko change krta hai
var a=[10,20,30,40,50]
a.forEach(
function(val){
console.log(val+2);
}
)
/*a k har ek element pr function apply ho like forEach mean hr ek function k agay val is liyai likha hai ta k value one by one val
main aay gi phir is pr function apply hoo ga jesay upper wala fnc agar a ko function k bahir print krain gy tou pata chalay ga k
org array main koi changing nhi hoti ayni k a sy val main a ki copy aati hai lekin agar a ko bhi sath change krna hoo toyu
kesay krny gy? This answer is not in this video*/
forin:(apply in objects )
var a={
name:"Ali",
age:"18",
city:"Sargodha"
}
//Syntax: for(var varName in Obj name)
for(var key in a){
console.log(key,a[key]);}
//key sy sirf name age city print hoo ga aur a[key] sy is k agay wala colum

Do While:
Asi jagha jahan ek bar kam az kam statement chalani hoo condition check hoonay sy pehly itna use nhi hoota
var i=1;
//agar i greater than 10 lekh doun phir bhi statment chalay gi
do{
console.log("hey");
i++
}
while(i<=10)
Call Back Functions:
Aisa Code jo bahad main chalta hai us main hum ek function dety hain k yai jb complete hoo jay tou fnc chal
jay aur yai function normal fnc hi hoota hai & isy call back function kehty hain
setTimeout(function(){
console.log("HY");
},2000)
First Class Function:
In first class function we can treat functions like a value like
Var a=function(){}
function abcd(a){
a();
}
abcd(function(){console.log("Hello")})
How Arrays Make Behind the Scence:
Hum jo array banatay hain wo array nhi hai type of sy check kr sktay hain wo object show hoti hai
var arr=[10,20,30,40];
var arr={
0=10
1=20
2=30
3=40}array store behind like an object
arr[-1]=2; // It is created a negative index with the upper program
Resulte:[10, 20, 30, 40, 50, -1: 2]

How To Delete Obj prop:


var ara={
name:"Ali",
age:18,
}
delete ara.age

You might also like