SlideShare a Scribd company logo
1 of 44
Tutorial-5
• if-else and switch case
• Loops - for , for-in, while, do while
• this in JS
• difference between JS this and other
langugage this
• consoles-log,error-warning
• local storage and session storage
• execution context
• Hoisting
• functions in JS
• How functions work in JS
• Scope chain & lexical scoping
• let vs const vs var
• block scope
• first class function
• closure
• settimeout with closure
• setimeInterval
• Interview Questions
Conditionals
• There are 2 major types of conditionals
⚬ if-else
⚬ switch-case
• if-else
⚬ syntax: if(condition){
⚬ someting
⚬ }else{
⚬ someting else
⚬ }
• if-else can be looped also
⚬ if-else-if
• else should always be with an if already
• ternary operator can be used for shorthand
notation:
• ternary operator is ?:
• else if can also be associated as one big
combination w.r.t. production developemn
• it's not good habbit to loop ternary operator -
coz it then compromise code readability
Swith-case
• its a single condition satisfactory conditional which helps in
satisfying any particular condition out of all
• if-else can be an alternate of switch-case but not vise-versa
• syntax:
Loops in JS
• We will look into following major kind of loops
in JS:
⚬ for
⚬ for-in
⚬ forEach
⚬ while
⚬ do-while
For loop
for-in
for-each
while-loop---->>
"this" in JavaScript
• this is more over like reference thing. Reference means -
reference to whatever data member i.e. either function or
variable in that scope
• We'll look following scenarios:
⚬ this inside object
⚬ this inside function
⚬ this inside function in presence of 'use strict' (use of call())
⚬ this inside of a function's function
⚬ this inside a function constructor
⚬ this inside a class constructor
web console
• It is the part of browser that helps the developer to interact
there website or web-app considering domains like network
requests, javascript, security errors, warnings, CSS etc.
console object
• It is majorly for printing any result in console environment of browser by usage of
some of following of its functions:
use strict
• Defines that JavaScript code should be executed
in "strict mode".
• With strict mode, you can not, for example, use
undeclared variables.
• All modern browsers support "use strict" except
Internet Explorer 9
alert (), prompt(), confirm()
• syntax:
⚬ alert("Hello! I am an alert box!!");
• syntax:
⚬ prompt("Please enter your name", "Harry Potter");
• synatx:
⚬ confirm("Press a button!");
• code:
⚬ <script>
⚬ const fn = window.prompt("message")
⚬ fn()
⚬ </script>
local storage vs session storage vs cookies
All 3 are nothing but mechanisms to sto store information as per requirement
• Now days only session storage & local storage are
used its bit rare and ONLY if needed then only
cookies are used on production development
• Fundtions used for
⚬ localstorage are:
■ localStorage.setItem("lastname", "Smith")
■ localStorage.getItem("lastname")
■ localStorage.removeItem("key")
■ localStorage.clear()
• SessionStorage functions are
⚬ sessionStorage.setItem("key", "value");
⚬ sessionStorage.getItem("key");
⚬ sessionStorage.removeItem("key");
⚬ sessionStorage.clear();
Execution Context
• Everything in JS executes inside execution
context
• it has two major parts
⚬ Memory component or variable
environment
■ it hold the variables and there values
in the form of key and values
⚬ Code component or thread of execution
■ it is where actually your code gets executed
• Hence,
⚬ NOTE:
■ JavaScript is synchronous single threaded
language
■ meaning: every code line is executed line by
line i.e. in the form of 1 thread or LOC at a
time
functions in JS
• we have learn that JS is nothing but the brain part
of website hence when we give any instruction to
JS that instruction is in the form of functions
• majorly we have 2 ways to write a function:
⚬ traditional way of writing a function i.e. ES-5
way to write a function using "function"
keyword
⚬ arrow function which came up in ES-6
Arrow function
ES-5 function
Hoisting in JS
meaning in English
• It says raise or haul up or jack up, hence in JS when
hoisting comes into picture it also do the same thing via
compiler
• Where interpreter if it executes a LOC and if we are
trying to use a function even befor we have used
then interpreter searches for that function in outer
scope & when it finds it up, then it hoist or jack or
hook up that function exisiting in outer scope
• Here we will see in code the hoisting behaviour
for variables , ES-5 functions & arrow functions
• Hoisting in javascript is considered with
declaration not with with initialization
⚬ which leads to different behavior of hoisting
of hoisting by using let or const and var
⚬ please checkout :
■ https://blog.bitsrc.io/hoisting-in-modern-
javascript-let-const-and-var-
b290405adfda
How functions work in JS
• Lets consider following code and match with our previous
Execution context diagram lets see how actually EC and CALL
STACK play there role in here
out-put
scope & scope -chain & lexical scope
• Scope- It is the are to which your declared
variable has access to or upto that scope you
can actually achieve that variable's value
• Scope chain- When we have more then one
lexical scopes assiciated to each other it leads to
form a scope chain
• Lexical scope - It is entire scope of itself + scope
of its lexical parent
lexical-scope
var - let - const and Temporal Deadzone
Interview question : Can let and const be hoisted
Answer: Not really because of Temporal Deadzone
• What is temporal deadzone or TDZ
⚬ Ans: the term to describe the state where variables
are un-reachable. They are in scope, but they aren't
declared.
• Where can we see TDZ
⚬ The let and const variables exist in the TDZ from the
start of their enclosing scope until they are declared.
• Refference Error:
⚬ When interpreter is not able to find required data
member due to absenece of it or if it is present in TDZ
⚬ Still upper part of that LOC will be executed
• Syntax Error:
⚬ When we disobey the syntax guidelines of JS
⚬ It'll stop execution of entire program at once
• Type Error:
⚬ When we disobey the "type" related guidelines of JS
■ e.g. reassigning a "const" variable
• const a = 10;
• a = 20
Block & Shadowing
• When we have to write a multiple LOC in
different lines then we make use of {} which is
termed as a block
• NOTE:
⚬ var is always global scoped
⚬ let and const are always block scoped
100
20
100
20
100
100
• Third snippet is significantly showing
"SHADOWING", where outer scoped variable value
is completely shadowed by inner one
• Shadowing is only leageal when shadowing and
shadowed variable has same type else it is called
as "Illegal shadowing"
• Possible shadowing scenarios are
⚬ var can be shadowed by any (var, let or const)
⚬ const and let cannot be shadowed by var
First class functions
• Before diving into the first class functions lets look into
followings :
⚬ What is function statement
⚬ What is function expression
⚬ What is the difference between function statement &
function expression
⚬ What is anonymous function
⚬ What is named expression
⚬ Difference between arguments & parameters
⚬ Now - What is first class function or first class citizens in JS
Function statement Function expression
Anonymous function
• You can see error in line 10 because
anonymous functions are only allowed to be
used when they are needed to be passed
Named expression
Parameters are received by called function &
argument are the values which are passed to
function
First class functions are the functions that is
actually taking the function as parameter and it
can also return a function too
Closures
• Closure in javascript is the power of a function when
accompanied with its lexical scope
• https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Closures
• Some usecases of closures are :
normal function exmaple-1
closure exmaple-2
FAMOUS INTERVIEW QUESTION ON CLOSURE
Basic introduction to setTimeout
Problem statement:
print 1 t0 5 after every 1-5 seconds i.e. 1 in 1 sec
and 2 in 2 sec and 3 in 3rd sec....
Expected solution
But output is wrong
Solution with let
Solution with var

More Related Content

What's hot

What's hot (20)

Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Introduction to JavaScript Programming
Introduction to JavaScript ProgrammingIntroduction to JavaScript Programming
Introduction to JavaScript Programming
 
Web development basics (Part-7)
Web development basics (Part-7)Web development basics (Part-7)
Web development basics (Part-7)
 
Java script
Java scriptJava script
Java script
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
introduction to javascript
introduction to javascriptintroduction to javascript
introduction to javascript
 
Placement and variable 03 (js)
Placement and variable 03 (js)Placement and variable 03 (js)
Placement and variable 03 (js)
 
JS Event Loop
JS Event LoopJS Event Loop
JS Event Loop
 
Basic JavaScript Tutorial
Basic JavaScript TutorialBasic JavaScript Tutorial
Basic JavaScript Tutorial
 
Javascript Basics
Javascript BasicsJavascript Basics
Javascript Basics
 
Web development basics (Part-5)
Web development basics (Part-5)Web development basics (Part-5)
Web development basics (Part-5)
 
Javascript
JavascriptJavascript
Javascript
 
Web development basics (Part-3)
Web development basics (Part-3)Web development basics (Part-3)
Web development basics (Part-3)
 
Java script ppt
Java script pptJava script ppt
Java script ppt
 
Getting started with typescript and angular 2
Getting started with typescript  and angular 2Getting started with typescript  and angular 2
Getting started with typescript and angular 2
 
Typescript
TypescriptTypescript
Typescript
 
Web development basics (Part-6)
Web development basics (Part-6)Web development basics (Part-6)
Web development basics (Part-6)
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
 
Javascript
JavascriptJavascript
Javascript
 
JavaScript - Chapter 3 - Introduction
 JavaScript - Chapter 3 - Introduction JavaScript - Chapter 3 - Introduction
JavaScript - Chapter 3 - Introduction
 

Similar to Web development basics (Part-4)

FRONTEND BOOTCAMP Session 2.pptx
FRONTEND BOOTCAMP Session 2.pptxFRONTEND BOOTCAMP Session 2.pptx
FRONTEND BOOTCAMP Session 2.pptxEhtesham46
 
gdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxgdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxsandeshshahapur
 
Looping and switch cases
Looping and switch casesLooping and switch cases
Looping and switch casesMeoRamos
 
JavaScript Comprehensive Overview
JavaScript Comprehensive OverviewJavaScript Comprehensive Overview
JavaScript Comprehensive OverviewMohamed Loey
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8Heartin Jacob
 
BITM3730Week6.pptx
BITM3730Week6.pptxBITM3730Week6.pptx
BITM3730Week6.pptxMattMarino13
 
Java Closures
Java ClosuresJava Closures
Java ClosuresBen Evans
 
Learn To Code: Introduction to java
Learn To Code: Introduction to javaLearn To Code: Introduction to java
Learn To Code: Introduction to javaSadhanaParameswaran
 
Javascripts hidden treasures BY - https://geekyants.com/
Javascripts hidden treasures            BY  -  https://geekyants.com/Javascripts hidden treasures            BY  -  https://geekyants.com/
Javascripts hidden treasures BY - https://geekyants.com/Geekyants
 
Hsc IT Chap 3. Advanced javascript-1.pdf
Hsc IT Chap 3. Advanced javascript-1.pdfHsc IT Chap 3. Advanced javascript-1.pdf
Hsc IT Chap 3. Advanced javascript-1.pdfAAFREEN SHAIKH
 
Java Basics for selenium
Java Basics for seleniumJava Basics for selenium
Java Basics for seleniumapoorvams
 
Web technologies-course 08.pptx
Web technologies-course 08.pptxWeb technologies-course 08.pptx
Web technologies-course 08.pptxStefan Oprea
 
Polymorphism Using C++
Polymorphism Using C++Polymorphism Using C++
Polymorphism Using C++PRINCE KUMAR
 
WT Module-3.pptx
WT Module-3.pptxWT Module-3.pptx
WT Module-3.pptxRamyaH11
 
Fundamentals of programming finals.ajang
Fundamentals of programming finals.ajangFundamentals of programming finals.ajang
Fundamentals of programming finals.ajangJaricka Angelyd Marquez
 

Similar to Web development basics (Part-4) (20)

FRONTEND BOOTCAMP Session 2.pptx
FRONTEND BOOTCAMP Session 2.pptxFRONTEND BOOTCAMP Session 2.pptx
FRONTEND BOOTCAMP Session 2.pptx
 
gdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxgdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptx
 
Looping and switch cases
Looping and switch casesLooping and switch cases
Looping and switch cases
 
JavaScript Comprehensive Overview
JavaScript Comprehensive OverviewJavaScript Comprehensive Overview
JavaScript Comprehensive Overview
 
Working With Concurrency In Java 8
Working With Concurrency In Java 8Working With Concurrency In Java 8
Working With Concurrency In Java 8
 
BITM3730Week6.pptx
BITM3730Week6.pptxBITM3730Week6.pptx
BITM3730Week6.pptx
 
Java Closures
Java ClosuresJava Closures
Java Closures
 
Learn To Code: Introduction to java
Learn To Code: Introduction to javaLearn To Code: Introduction to java
Learn To Code: Introduction to java
 
Unit II.pptx
Unit II.pptxUnit II.pptx
Unit II.pptx
 
Javascripts hidden treasures BY - https://geekyants.com/
Javascripts hidden treasures            BY  -  https://geekyants.com/Javascripts hidden treasures            BY  -  https://geekyants.com/
Javascripts hidden treasures BY - https://geekyants.com/
 
Core JavaScript
Core JavaScriptCore JavaScript
Core JavaScript
 
Codemotion 2015 spock_workshop
Codemotion 2015 spock_workshopCodemotion 2015 spock_workshop
Codemotion 2015 spock_workshop
 
C language (Part 2)
C language (Part 2)C language (Part 2)
C language (Part 2)
 
Java 9, JShell, and Modularity
Java 9, JShell, and ModularityJava 9, JShell, and Modularity
Java 9, JShell, and Modularity
 
Hsc IT Chap 3. Advanced javascript-1.pdf
Hsc IT Chap 3. Advanced javascript-1.pdfHsc IT Chap 3. Advanced javascript-1.pdf
Hsc IT Chap 3. Advanced javascript-1.pdf
 
Java Basics for selenium
Java Basics for seleniumJava Basics for selenium
Java Basics for selenium
 
Web technologies-course 08.pptx
Web technologies-course 08.pptxWeb technologies-course 08.pptx
Web technologies-course 08.pptx
 
Polymorphism Using C++
Polymorphism Using C++Polymorphism Using C++
Polymorphism Using C++
 
WT Module-3.pptx
WT Module-3.pptxWT Module-3.pptx
WT Module-3.pptx
 
Fundamentals of programming finals.ajang
Fundamentals of programming finals.ajangFundamentals of programming finals.ajang
Fundamentals of programming finals.ajang
 

Recently uploaded

Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 

Recently uploaded (20)

Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 

Web development basics (Part-4)

  • 1. Tutorial-5 • if-else and switch case • Loops - for , for-in, while, do while • this in JS • difference between JS this and other langugage this • consoles-log,error-warning • local storage and session storage • execution context • Hoisting • functions in JS • How functions work in JS • Scope chain & lexical scoping • let vs const vs var • block scope • first class function • closure • settimeout with closure • setimeInterval • Interview Questions
  • 2. Conditionals • There are 2 major types of conditionals ⚬ if-else ⚬ switch-case • if-else ⚬ syntax: if(condition){ ⚬ someting ⚬ }else{ ⚬ someting else ⚬ }
  • 3. • if-else can be looped also ⚬ if-else-if • else should always be with an if already • ternary operator can be used for shorthand notation: • ternary operator is ?: • else if can also be associated as one big combination w.r.t. production developemn • it's not good habbit to loop ternary operator - coz it then compromise code readability
  • 4. Swith-case • its a single condition satisfactory conditional which helps in satisfying any particular condition out of all • if-else can be an alternate of switch-case but not vise-versa • syntax:
  • 5. Loops in JS • We will look into following major kind of loops in JS: ⚬ for ⚬ for-in ⚬ forEach ⚬ while ⚬ do-while
  • 9. "this" in JavaScript • this is more over like reference thing. Reference means - reference to whatever data member i.e. either function or variable in that scope • We'll look following scenarios: ⚬ this inside object ⚬ this inside function ⚬ this inside function in presence of 'use strict' (use of call()) ⚬ this inside of a function's function ⚬ this inside a function constructor ⚬ this inside a class constructor
  • 10. web console • It is the part of browser that helps the developer to interact there website or web-app considering domains like network requests, javascript, security errors, warnings, CSS etc. console object • It is majorly for printing any result in console environment of browser by usage of some of following of its functions:
  • 11. use strict • Defines that JavaScript code should be executed in "strict mode". • With strict mode, you can not, for example, use undeclared variables. • All modern browsers support "use strict" except Internet Explorer 9
  • 12. alert (), prompt(), confirm() • syntax: ⚬ alert("Hello! I am an alert box!!"); • syntax: ⚬ prompt("Please enter your name", "Harry Potter"); • synatx: ⚬ confirm("Press a button!"); • code: ⚬ <script> ⚬ const fn = window.prompt("message") ⚬ fn() ⚬ </script>
  • 13. local storage vs session storage vs cookies All 3 are nothing but mechanisms to sto store information as per requirement
  • 14. • Now days only session storage & local storage are used its bit rare and ONLY if needed then only cookies are used on production development • Fundtions used for ⚬ localstorage are: ■ localStorage.setItem("lastname", "Smith") ■ localStorage.getItem("lastname") ■ localStorage.removeItem("key") ■ localStorage.clear()
  • 15. • SessionStorage functions are ⚬ sessionStorage.setItem("key", "value"); ⚬ sessionStorage.getItem("key"); ⚬ sessionStorage.removeItem("key"); ⚬ sessionStorage.clear();
  • 16. Execution Context • Everything in JS executes inside execution context • it has two major parts ⚬ Memory component or variable environment ■ it hold the variables and there values in the form of key and values ⚬ Code component or thread of execution
  • 17. ■ it is where actually your code gets executed • Hence, ⚬ NOTE: ■ JavaScript is synchronous single threaded language ■ meaning: every code line is executed line by line i.e. in the form of 1 thread or LOC at a time
  • 18. functions in JS • we have learn that JS is nothing but the brain part of website hence when we give any instruction to JS that instruction is in the form of functions • majorly we have 2 ways to write a function: ⚬ traditional way of writing a function i.e. ES-5 way to write a function using "function" keyword ⚬ arrow function which came up in ES-6
  • 20. Hoisting in JS meaning in English • It says raise or haul up or jack up, hence in JS when hoisting comes into picture it also do the same thing via compiler
  • 21. • Where interpreter if it executes a LOC and if we are trying to use a function even befor we have used then interpreter searches for that function in outer scope & when it finds it up, then it hoist or jack or hook up that function exisiting in outer scope • Here we will see in code the hoisting behaviour for variables , ES-5 functions & arrow functions
  • 22. • Hoisting in javascript is considered with declaration not with with initialization ⚬ which leads to different behavior of hoisting of hoisting by using let or const and var ⚬ please checkout : ■ https://blog.bitsrc.io/hoisting-in-modern- javascript-let-const-and-var- b290405adfda
  • 24. • Lets consider following code and match with our previous Execution context diagram lets see how actually EC and CALL STACK play there role in here out-put
  • 25. scope & scope -chain & lexical scope • Scope- It is the are to which your declared variable has access to or upto that scope you can actually achieve that variable's value • Scope chain- When we have more then one lexical scopes assiciated to each other it leads to form a scope chain • Lexical scope - It is entire scope of itself + scope of its lexical parent
  • 27. var - let - const and Temporal Deadzone
  • 28. Interview question : Can let and const be hoisted Answer: Not really because of Temporal Deadzone • What is temporal deadzone or TDZ ⚬ Ans: the term to describe the state where variables are un-reachable. They are in scope, but they aren't declared. • Where can we see TDZ ⚬ The let and const variables exist in the TDZ from the start of their enclosing scope until they are declared.
  • 29. • Refference Error: ⚬ When interpreter is not able to find required data member due to absenece of it or if it is present in TDZ ⚬ Still upper part of that LOC will be executed • Syntax Error: ⚬ When we disobey the syntax guidelines of JS ⚬ It'll stop execution of entire program at once • Type Error: ⚬ When we disobey the "type" related guidelines of JS ■ e.g. reassigning a "const" variable • const a = 10; • a = 20
  • 30. Block & Shadowing • When we have to write a multiple LOC in different lines then we make use of {} which is termed as a block • NOTE: ⚬ var is always global scoped ⚬ let and const are always block scoped
  • 32. • Third snippet is significantly showing "SHADOWING", where outer scoped variable value is completely shadowed by inner one • Shadowing is only leageal when shadowing and shadowed variable has same type else it is called as "Illegal shadowing" • Possible shadowing scenarios are ⚬ var can be shadowed by any (var, let or const) ⚬ const and let cannot be shadowed by var
  • 33. First class functions • Before diving into the first class functions lets look into followings : ⚬ What is function statement ⚬ What is function expression ⚬ What is the difference between function statement & function expression ⚬ What is anonymous function ⚬ What is named expression ⚬ Difference between arguments & parameters ⚬ Now - What is first class function or first class citizens in JS
  • 34. Function statement Function expression Anonymous function • You can see error in line 10 because anonymous functions are only allowed to be used when they are needed to be passed Named expression
  • 35. Parameters are received by called function & argument are the values which are passed to function
  • 36. First class functions are the functions that is actually taking the function as parameter and it can also return a function too
  • 37. Closures • Closure in javascript is the power of a function when accompanied with its lexical scope • https://developer.mozilla.org/en- US/docs/Web/JavaScript/Closures • Some usecases of closures are :
  • 40. FAMOUS INTERVIEW QUESTION ON CLOSURE Basic introduction to setTimeout
  • 41. Problem statement: print 1 t0 5 after every 1-5 seconds i.e. 1 in 1 sec and 2 in 2 sec and 3 in 3rd sec....