2. callback function
• A callback function is a function which is:
• accessible by another function, and
• is invoked after the first function if that first function completes
• This construct is very useful for asynchronous behaviour where we want an
activity to take place whenever a previous event completes.
3. • In JavaScript, pass a function as an argument to a function. This function
that is passed as an argument inside of another function is called a callback
function. For example,
<html>
<head>
<script>
function greet(name, callback)
{ document.write("<h1>" + 'Hi' + ' ' + name + "</h1>");
callback();
}
function callMe()
{ document.write("<h1>" + 'I am callback function' + "</h1>");
}
greet('Peter', callMe);
</script> </head> <body> </body> </html>
4. Benefit of Callback Function
• wait for the result of a previous function call and then execute another
function call.
• In this example, we are going to use the setTimeout() method to mimic the
program that takes time to execute, such as data coming from the server.
<script>
alert(1);
setTimeout(() => alert(2), 0);
alert(3);
</script>
5. Two types of programming models-
Synchronous and Asynchronous models
•Synchronous tasks happen in order — you must finish
task one before moving on to the next.
•Asynchronous tasks can be executed in any order, or
even simultaneously.
6. JavaScript
•JavaScript is dynamically typed single-threaded
interpreted languages for the Web.
•Dynamically typed language which means a variable can
hold any data type like String or Number in its lifetime and
JavaScript interpreter won’t complain about it.
•It’s single-threaded which means your JavaScript code runs
synchronously or sequentially line by line. It’s interpreted
which means you don’t need to compile your JavaScript
code.
7. JavaScript
•JavaScript Interpreter also called a JavaScript Engine.
•V8 is the JavaScript engine designed by Google and
used in the Google Chrome browser while
SpiderMonkey is a JavaScript engine developed by
Mozilla for their Firefox browser.
8. •Javascript is a single-threaded language, meaning
that just one line of code may be run at once.
•Javascript is single-threaded because, originally, it was
only a web browser scripting language created to
serve the needs of a single user on a single window
of the browser, eliminating the need for
multithreading.
9. What is server-side JavaScript?
•JavaScript is a single-threaded language, it knows
how to get things done one at a time.
•It can’t do asynchronous tasks or run JavaScript code
in multiple threads for efficiency.
•JavaScript is abbreviated as JS or .js/.JS (dot J S) to
state that an entity is related to JavaScript, like
Node.js or ReactJS or AngularJS.
10. What is server-side JavaScript?
•JavaScript is used on the web, it needs to be
secure. Hence, using JavaScript, you can’t access the
computer it is running on, like File System, IO,
Networking, etc.
11. What is server-side JavaScript?
• Web APIs sometimes do their job in separate thread
allowing other JavaScript code to run normally while the
job is running in the background.
• Once the job is done, it then informs the main JavaScript
thread.
• For example, setTimeout(callback, delay) function is not
part of ECMAScript specification, it is provided by the
browser to perform an asynchronous operation. The
callback function is executed in the main JavaScript thread
once delay milliseconds has elapsed.
13. The concept of server-side JavaScript
•You can take any JavaScript engine, wrap inside an
application that gives a clean interface to take the user’s
JavaScript code and execute it in the JavaScript engine.
•You can also provide APIs to perform operations like File
System IO, Networking, etc. which do not run on
JavaScript engine.
15. Server-side JS Framework
•It provide tools and libraries that simplify
common web development tasks,
•including routing URLs to appropriate handlers,
interacting with databases, supporting sessions
and user authorization, formatting output (e.g.
HTML, JSON, XML), and improving security
against web attacks.
16. Introduction to Node.js
•Node.js is an open-source server side runtime environment
built on Chrome's V8 JavaScript engine.
•It provides an event driven, non-blocking
(asynchronous) I/O and cross-platform runtime
environment for building highly scalable server-
side applications using JavaScript.
17. Node.js
•Node.js is nothing but JavaScript running on the
server
•Node JS is asynchronous and event-driven
JavaScript runtime build for scalable network
applications.
•Although JavaScript in web browser is single thread
and synchronous,
•Node JS is asynchronous and can use multi threads
for I/O or others tasks in background.
18. Introduction to Node.js
• Node.js is an open-source and cross-platform runtime
environment for executing JavaScript code outside a
browser.
• NodeJS is not a framework and it’s not a programming
language.
Node.js = Runtime Environment + JavaScript Library
19. Needs of Node.js
• Enables JavaScript to run outside the browser
• Makes use of Google's V8 VM for interpreting JavaScript
• Additional modules which simplifies JavaScript development
• It’s a runtime and also a library!
• JavaScript is an event-driven language, and Node takes this as an advantage to
produce highly scalable servers, using an architecture called an event loop.
• For Creating High Performance Servers.
• Non-Blocking I/O.
• Event and Callback based.
• Only one Thread and one call stack
20. Node JS based applications
• Web Application Development.
• Hybrid Apps Development. (Both Desktop & Mobile)
• API Development.
• Chat Applications.
• Streaming Services for Video and Audio.
21. Node JS Based Web Servers
• Netflix
• Linkedin
• Uber
• Paypal
• Nasa
• Medium
• Slack
• Twitter
• Free Charge
• Flipkart Seller
22. Node JS Based Desktop Applications
• VS Code
• Brackets
• Atom
• MongoDB Compass
• Postman
• Discord
• Dropbox
• Figma
• Github Desktop
• Microsoft Teams
• Skype
• Wordpress Desktop
• Whatsapp for Desktop
• Facebook Messenger for Desktop
24. tools/SDK are required for developing a
Node.js application on any platform
•Node.js
•Node Package Manager (NPM)
•IDE (Integrated Development Environment) or
TextEditor
25. Install Node.js on Windows
•official web site https://nodejs.org.
•It will automatically detect OS and display download
link as per your Operating System.
•For example, it will display following download link for
64 bit Windows OS.
36. After installation, verify the Node.js installation using terminal window and enter
the following command. It will display the version number of Node.js installed on
your Mac.
$ node -v
38. Node.js comes with virtual environment called REPL (aka Node shell). REPL stands for Read-Eval-Print-Loop. It is a quick and
easy way to test simple Node.js/JavaScript code.
To launch the REPL (Node shell), open command prompt (in Windows) or terminal (in Mac or UNIX/Linux) and type node
as shown below. It will change the prompt to > in Windows and MAC.
40. If you need to write multi line JavaScript expression or function then just press Enter whenever you want to write
something in the next line as a continuation of your code. The REPL terminal will display three dots (...), it means
you can continue on next line. Write .break to get out of continuity mode.
41. To exit from the REPL terminal, press Ctrl + C
twice or write .exit and press Enter.
42. JavaScript file
• 1. create a sample.js file through text editior / notepad
• 2. with in sample.js file - type
console.log("Hello World");
3. Save your file again
4. Open cmd - Move to file location
5. Executing the node filename
node sample.js