SlideShare a Scribd company logo
JavaScript
Megha V
Asst. Professor
Dept. of IT
Kannur University
14-10-2022 1
meghav@kannuruniv.ac.in
Introduction to JavaScript
• JavaScript is a lightweight, cross-platform, and interpreted compiled
programming language which is also known as the scripting language for
webpages.
• It is well-known for the development of web pages, many non-browser
environments also use it.
• A scripting language is a programming language that employs a high-level
construct to interpret and execute one command at a time.
• In general, scripting languages are easier to learn and faster to code in than
more structured and compiled languages such as C and C++.
14-10-2022 2
meghav@kannuruniv.ac.in
Introduction to JavaScript
• JavaScript can be used for Client-side developments as well
as Server-side developments.
Client-side Programming :
• It is the program that runs on the client machine (browser) and
deals with the user interface/display and any other processing
that can happen on client machine like reading/writing cookies.
1) Interact with temporary storage
2) Make interactive web pages
3) Interact with local storage
4) Sending request for data to server
5) Send request to server
6) work as an interface between server and user
14-10-2022 3
meghav@kannuruniv.ac.in
• The Programming languages for client-side programming are :
1) JavaScript
2) VBScript
3) HTML
4) CSS
5) AJAX
14-10-2022 4
meghav@kannuruniv.ac.in
Server-side Programming :
• It is the program that runs on server dealing with the generation
of content of web page.
1) Querying the database
2) Operations over databases
3) Access/Write a file on server.
4) Interact with other servers.
5) Structure web applications.
6) Process user input.
• For example if user input is a text in search box, run a search
algorithm on data stored on server and send the results.
14-10-2022 5
meghav@kannuruniv.ac.in
• Examples :
The Programming languages for server-side programming are :
1) PHP
2) C++
3) Java and JSP
4) Python
5) Ruby on Rails
14-10-2022 6
meghav@kannuruniv.ac.in
• Javascript is both imperative and declarative type of language.
• JavaScript contains a standard library of objects,
like Array, Date, and Math, and a core set of language
elements like operators, control structures, and statements.
14-10-2022 7
meghav@kannuruniv.ac.in
Client-side:
• It supplies objects to control a browser and its Document Object Model
(DOM).
• When a web page is loaded, the browser creates
a Document Object Model of the page.
The HTML DOM is a standard object model and programming interface for
HTML. It defines:
• The HTML elements as objects
• The properties of all HTML elements
• The methods to access all HTML elements
• The events for all HTML elements
14-10-2022 8
meghav@kannuruniv.ac.in
• Like if client-side extensions allow an application to place elements on
an HTML form and respond to user events such as mouse clicks, form
input, and page navigation.
• Useful libraries for the client-side are AngularJS, ReactJS, VueJS and
so many others.
14-10-2022 meghav@kannuruniv.ac.in 9
Server-side:
• It supplies objects relevant to running JavaScript on a server.
• Like if the server-side extensions allow an application to
communicate with a database, and provide continuity of
information from one invocation to another of the application, or
perform file manipulations on a server.
• The useful framework which is the most famous these days
is node.js.
14-10-2022 10
meghav@kannuruniv.ac.in
Imperative language
• In this type of language we are mostly concern about how it is
to be done.
• It simply control the flow of computation .
• The procedural programming approach , object, oriented
approach comes under this.
14-10-2022 11
meghav@kannuruniv.ac.in
Declarative programming –
• In this type of language we are concern about how it is to be
done , basically here logical computation require .
• Here main goal is to describe the desired result without direct
dictation on how to get it like arrow function do.
14-10-2022 12
meghav@kannuruniv.ac.in
JavaScript can be added to your HTML file
in two ways:
• Internal JS: We can add JavaScript directly to our HTML file by
writing the code inside the <script> tag.
• The <script> tag can either be placed inside the <head> or the
<body> tag according to the requirement.
• External JS: We can write JavaScript code in other file having
an extension .js and then link this file inside the <head> tag of
the HTML file in which we want to add this code.
14-10-2022 13
meghav@kannuruniv.ac.in
Syntax:
<script>
// JavaScript Code
</script>
14-10-2022 14
meghav@kannuruniv.ac.in
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<title>
Basic Example to Describe JavaScript
</title>
</head>
<body>
<!-- JavaScript code can be embedded inside
head section or body section -->
<script>
console.log("Welcome to GeeksforGeeks");
</script>
</body>
</html>
14-10-2022 15
meghav@kannuruniv.ac.in
External JS
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
• External scripts are practical when the same code is used in many
different web pages.
• JavaScript files have the file extension .js.
• To use an external script, put the name of the script file in the src
(source) attribute of a <script> tag:
<script src="myScript.js"></script>
14-10-2022 meghav@kannuruniv.ac.in 16
History of JavaScript:
• It was created in 1995 by Brendan Eich while he was an
engineer at Netscape.
• It was originally going to be named LiveScript but was
renamed.
• Unlike most programming languages, the JavaScript language
has no concept of input or output.
• It is designed to run as a scripting language in a host
environment, and it is up to the host environment to provide
mechanisms for communicating with the outside world.
• The most common host environment is the browser.
14-10-2022 17
meghav@kannuruniv.ac.in
Features of JavaScript:
• According to a recent survey conducted by Stack Overflow,
JavaScript is the most popular language on earth.
• With advances in browser technology and JavaScript having
moved into the server with Node.js and other frameworks,
JavaScript is capable of so much more.
• Here are a few things that we can do with JavaScript:
14-10-2022 18
meghav@kannuruniv.ac.in
• JavaScript was created in the first place for DOM manipulation.
• Earlier websites were mostly static, after JS was created
dynamic Web sites were made.
• Functions in JS are objects.
• They may have properties and methods just like another object.
• They can be passed as arguments in other functions.
• Can handle date and time.
• Performs Form Validation although the forms are created using
HTML.
• No compiler is needed.
14-10-2022 19
meghav@kannuruniv.ac.in
Variables and Datatypes in JavaScript
Datatypes in JavaScript
• There are majorly two types of languages.
• First, one is Statically typed language where each variable
and expression type is already known at compile time.
• Once a variable is declared to be of a certain data type, it
cannot hold values of other data types.
• Example: C, C++, Java.
14-10-2022 20
meghav@kannuruniv.ac.in
// Java(Statically typed)
int x = 5 // variable x is of type int and it will not
store any other type.
string y = 'abc' // type string and will only accept
string values
Other, Dynamically typed languages: These languages can
receive different data types over time. For example- Ruby, Python,
JavaScript, etc.
// Javascript(Dynamically typed)
var x = 5; // can store an integer
var name = 'string'; // can also store a string.
14-10-2022 21
meghav@kannuruniv.ac.in
• JavaScript is a dynamically typed (also called loosely typed)
scripting language.
• That is, in JavaScript variables can receive different data types
over time.
• Datatypes are basically typed data that can be used and
manipulated in a program.
14-10-2022 22
meghav@kannuruniv.ac.in
• The latest ECMAScript(ES6)-European Computer Manufacturers Association
Script standard defines following data types:
• Out of which six data types are Primitive(predefined).
•Numbers: Represent both integer and floating-point numbers. Example: 5,
6.5, 7 etc.
•String: A string is a sequence of characters. In JavaScript, strings can be
enclosed within the single or double quotes. Example: “Hello GeeksforGeeks”
etc.
•Boolean: Represent a logical entity and can have two values: true or false.
•Null: This type has only one value : null.
•Undefined: A variable that has not been assigned a value is undefined.
14-10-2022 23
meghav@kannuruniv.ac.in
• Symbol: Unlike other primitive data types, it does not have any
literal form.
• It is a built-in object whose constructor returns a symbol-that is
unique.
• bigint: The bigint type represents the whole numbers that are
larger than 253-1.
• To form a bigint literal number, you append the letter n at the
end of the number.
• Object: It is the most important data-type and forms the building
blocks for modern JavaScript.
14-10-2022 24
meghav@kannuruniv.ac.in
Variables in JavaScript:
• Variables in JavaScript are containers that hold reusable data. It
is the basic unit of storage in a program.
• The value stored in a variable can be changed during program
execution.
• A variable is only a name given to a memory location, all the
operations done on the variable effects that memory location.
• In JavaScript, all the variables must be declared before they
can be used.
14-10-2022 25
meghav@kannuruniv.ac.in
• Before ES2015, JavaScript variables were solely declared using the var
keyword followed by the name of the variable and semi-colon.
• Below is the syntax to create variables in JavaScript:
var var_name;
var x;
• The var_name is the name of the variable which should be defined by
the user and should be unique.
• These types of names are also known as identifiers.
14-10-2022 26
meghav@kannuruniv.ac.in
• The rules for creating an identifier in JavaScript are, the name of
the identifier should not be any pre-defined word(known as
keywords), the first character must be a letter, an underscore (_),
or a dollar sign ($).
• Subsequent characters may be any letter or digit or an
underscore or dollar sign.
• Notice in the above code sample, we didn’t assign any
values to the variables. We are only saying they exist. If you
were to look at the value of each variable in the above code
sample, it would be undefined.
• We can initialize the variables either at the time of
declaration or also later when we want to use them.
14-10-2022 27
meghav@kannuruniv.ac.in
Below are some examples of declaring and
initializing variables in JavaScript:
// declaring single variable
var name;
// declaring multiple variables
var name, title, num;
// initializing variables
var name = "Harsh";
name = “Anu";
14-10-2022 28
meghav@kannuruniv.ac.in
JavaScript is also known as untyped language.
This means, that once a variable is created in JavaScript
using the keyword var, we can store any type of value in this
variable supported by JavaScript. Below is the example for
this:
// creating variable to store a number
var num = 5;
// store string in the variable num
num = "GeeksforGeeks";
14-10-2022 29
meghav@kannuruniv.ac.in
• The above example executes well without any error in JavaScr
unlike other programming languages.
• Variables in JavaScript can also evaluate simple mathematical
expressions and assume their value.
// storing a mathematical expression
var x = 5 + 10 + 1;
console.log(x); // 16
14-10-2022 30
meghav@kannuruniv.ac.in
• After ES2015, we now have two new variable containers: let and const.
• The variable type Let shares lots of similarities with var but unlike var, it
has scope constraints.
• To know more about them visit let vs var.
• Let’s make use of the let variable:
// let variable
let x; // undefined
let name = 'Mukul’;
// can also declare multiple values
let a=1,b=2,c=3;
// assignment
let a = 3;
a = 4; // works same as var
14-10-2022 31
meghav@kannuruniv.ac.in
Const is another variable type assigned to data whose value
and will not change throughout the script.
// const variable
const name = 'Mukul’;
name = 'Mayank'; // will give Assignment to constant error.
14-10-2022 32
meghav@kannuruniv.ac.in
Variable Scope in Javascript
• Scope of a variable is the part of the program from where the
variable may directly be accessible.
In JavaScript, there are two types of scopes:
1.Global Scope – Scope outside the outermost function attached
to Window.
2.Local Scope – Inside the function being executed.
14-10-2022 33
meghav@kannuruniv.ac.in
JavaScript
let globalVar = "This is a global variable";
function fun() {
let localVar = "This is a local variable";
console.log(globalVar);
console.log(localVar);
}
fun();
14-10-2022 34
meghav@kannuruniv.ac.in
Output
14-10-2022 35
meghav@kannuruniv.ac.in
• When we execute the function fun(), the output shows that both
global, and local variables, are accessible inside the function as
we are able to console.log them.
• This shows that inside the function we have access to both
global variables (declared outside the function) and local
variables (declared inside the function).
• Let’s move the console.log statements outside the function and
put them just after calling the function.
14-10-2022 36
meghav@kannuruniv.ac.in
JavaScript
let globalVar = "This is a global variable";
function fun() {
let localVar = "This is a local variable";
}
fun();
console.log(globalVar);
console.log(localVar);
14-10-2022 37
meghav@kannuruniv.ac.in
Output
14-10-2022 38
meghav@kannuruniv.ac.in
• We are still able to see the value of the global variable, but for
the local variable, console.log throws an error.
• This is because now the console.log statements are present in
global scope where they have access to global variables but
cannot access the local variables.
• Also, any variable defined in a function with the same name as
a global variable takes precedence over the global variable,
shadowing it.
14-10-2022 39
meghav@kannuruniv.ac.in
Reference
1. Jeffrey C. Jackson, Web Technologies: A Computer Science Perspective, Prentice Hall
2. David Flanagan, JavaScript: The Definitive Guide, 6th Edn. O'Reilly Media.2011
3. Bob Breedlove, et al, Web Programming Unleashed, Sams Net Publishing, 1stEdn
4. Steven Holzner, PHP: The Complete Reference, McGraw Hill Professional, 2008
5. Steve Suehring, Tim Converse, Joyce Park, PHP6 and MY SQL Bible, John Wiley & Sons,2009
6. Pedro Teixeira, Instant Node.js Starter, Packt Publishing Ltd., 2013
7. Anthony T. HoldenerIII, Ajax: The Definitive Guide, O’Reilly Media, 2008
8. Nirav Mehta, Choosing an Open Source CMS: Beginner's Guide Packt Publishing Ltd, 2009
9. James Snell, Programming Web Services with SOAP, O'Reilly 2002
10. Jacob Lett , Bootstrap Reference Guide, Bootstrap Creative 2018
11. Maximilian Schwarzmüller, Progressive Web Apps (PWA) - The Complete Guide, Packt Publishing
2018
12. Mahesh Panhale, Beginning Hybrid Mobile Application Development, Apress 2016
14-10-2022 40
meghav@kannuruniv.ac.in

More Related Content

What's hot

JavaScript - Chapter 3 - Introduction
 JavaScript - Chapter 3 - Introduction JavaScript - Chapter 3 - Introduction
JavaScript - Chapter 3 - Introduction
WebStackAcademy
 
Javascript
JavascriptJavascript
Javascript
guest03a6e6
 
Javascript
JavascriptJavascript
Javascript
Manav Prasad
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypesVarun C M
 
Web Development Presentation
Web Development PresentationWeb Development Presentation
Web Development Presentation
TurnToTech
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
Walid Ashraf
 
jQuery
jQueryjQuery
Javascript functions
Javascript functionsJavascript functions
Javascript functions
Alaref Abushaala
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
Sehwan Noh
 
Javascript
JavascriptJavascript
Javascript
Rajavel Dhandabani
 
Javascript Basic
Javascript BasicJavascript Basic
Javascript Basic
Kang-min Liu
 
Presentation of bootstrap
Presentation of bootstrapPresentation of bootstrap
Presentation of bootstrap
1amitgupta
 
Span and Div tags in HTML
Span and Div tags in HTMLSpan and Div tags in HTML
Span and Div tags in HTML
Biswadip Goswami
 
CSS Basics
CSS BasicsCSS Basics
CSS Basics
Sanjeev Kumar
 
Javascript
JavascriptJavascript
Javascript
Nagarajan
 
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript
pcnmtutorials
 
CSS Unit I - Basics of JavaScript Programming
CSS Unit I - Basics of JavaScript ProgrammingCSS Unit I - Basics of JavaScript Programming
CSS Unit I - Basics of JavaScript Programming
RahulTamkhane
 

What's hot (20)

Java script
Java scriptJava script
Java script
 
JavaScript - Chapter 3 - Introduction
 JavaScript - Chapter 3 - Introduction JavaScript - Chapter 3 - Introduction
JavaScript - Chapter 3 - Introduction
 
Javascript
JavascriptJavascript
Javascript
 
Javascript
JavascriptJavascript
Javascript
 
Javascript variables and datatypes
Javascript variables and datatypesJavascript variables and datatypes
Javascript variables and datatypes
 
Web Development Presentation
Web Development PresentationWeb Development Presentation
Web Development Presentation
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
 
jQuery
jQueryjQuery
jQuery
 
Javascript functions
Javascript functionsJavascript functions
Javascript functions
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
 
Javascript
JavascriptJavascript
Javascript
 
Javascript Basic
Javascript BasicJavascript Basic
Javascript Basic
 
Presentation of bootstrap
Presentation of bootstrapPresentation of bootstrap
Presentation of bootstrap
 
Span and Div tags in HTML
Span and Div tags in HTMLSpan and Div tags in HTML
Span and Div tags in HTML
 
Js ppt
Js pptJs ppt
Js ppt
 
CSS Basics
CSS BasicsCSS Basics
CSS Basics
 
Javascript
JavascriptJavascript
Javascript
 
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript
2. Classes | Object Oriented Programming in JavaScript | ES6 | JavaScript
 
CSS Unit I - Basics of JavaScript Programming
CSS Unit I - Basics of JavaScript ProgrammingCSS Unit I - Basics of JavaScript Programming
CSS Unit I - Basics of JavaScript Programming
 
Javascript
JavascriptJavascript
Javascript
 

Similar to Introduction to JavaScript

Full Stack Web Development
Full Stack Web DevelopmentFull Stack Web Development
Full Stack Web Development
SWAGATHCHOWDARY1
 
Meetup. Technologies Intro for Non-Tech People
Meetup. Technologies Intro for Non-Tech PeopleMeetup. Technologies Intro for Non-Tech People
Meetup. Technologies Intro for Non-Tech People
IT Arena
 
MERN PPT
MERN PPTMERN PPT
JS BASICS JAVA SCRIPT SCRIPTING
JS BASICS JAVA SCRIPT SCRIPTINGJS BASICS JAVA SCRIPT SCRIPTING
JS BASICS JAVA SCRIPT SCRIPTING
Arulkumar
 
Javascript 01 (js)
Javascript 01 (js)Javascript 01 (js)
Javascript 01 (js)
AbhishekMondal42
 
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
AAFREEN SHAIKH
 
Java for C++ programers
Java for C++ programersJava for C++ programers
Java for C++ programers
Salahaddin University-Erbil
 
Skills Required For Full Stack Developer.pdf
Skills Required For Full Stack Developer.pdfSkills Required For Full Stack Developer.pdf
Skills Required For Full Stack Developer.pdf
Uncodemy
 
Software Engineering 2014
Software Engineering 2014Software Engineering 2014
Software Engineering 2014
Shuichi Kurabayashi
 
DOT NET TRaining
DOT NET TRainingDOT NET TRaining
DOT NET TRaining
sunil kumar
 
Apache Drill (ver. 0.2)
Apache Drill (ver. 0.2)Apache Drill (ver. 0.2)
Apache Drill (ver. 0.2)
Camuel Gilyadov
 
C# vs Java What are The Differences.pdf
C# vs Java What are The Differences.pdfC# vs Java What are The Differences.pdf
C# vs Java What are The Differences.pdf
christiemarie4
 
INDUSTRIAL TRAINING Presentation on Web Development. (2).pptx
INDUSTRIAL TRAINING Presentation on Web Development. (2).pptxINDUSTRIAL TRAINING Presentation on Web Development. (2).pptx
INDUSTRIAL TRAINING Presentation on Web Development. (2).pptx
12KritiGaneriwal
 
Demo Lecture 01 Notes.pptx by Sabki Kaksha
Demo Lecture 01 Notes.pptx by Sabki KakshaDemo Lecture 01 Notes.pptx by Sabki Kaksha
Demo Lecture 01 Notes.pptx by Sabki Kaksha
GandhiSarthak
 
Demo Lecture 01 Notes paid , course notes
Demo Lecture 01 Notes paid , course notesDemo Lecture 01 Notes paid , course notes
Demo Lecture 01 Notes paid , course notes
GandhiSarthak
 
Web-Development-ppt (1).pptx
Web-Development-ppt (1).pptxWeb-Development-ppt (1).pptx
Web-Development-ppt (1).pptx
RaihanUddin57
 
Beginners Node.js
Beginners Node.jsBeginners Node.js
Beginners Node.js
Khaled Mosharraf
 
Product Camp Silicon Valley 2018 - PM Technical Skills
Product Camp Silicon Valley 2018 - PM Technical SkillsProduct Camp Silicon Valley 2018 - PM Technical Skills
Product Camp Silicon Valley 2018 - PM Technical Skills
Sandeep Adwankar
 
JavaScript New Tutorial Class XI and XII.pptx
JavaScript New Tutorial Class XI and XII.pptxJavaScript New Tutorial Class XI and XII.pptx
JavaScript New Tutorial Class XI and XII.pptx
rish15r890
 

Similar to Introduction to JavaScript (20)

Full Stack Web Development
Full Stack Web DevelopmentFull Stack Web Development
Full Stack Web Development
 
Meetup. Technologies Intro for Non-Tech People
Meetup. Technologies Intro for Non-Tech PeopleMeetup. Technologies Intro for Non-Tech People
Meetup. Technologies Intro for Non-Tech People
 
MERN PPT
MERN PPTMERN PPT
MERN PPT
 
Comp102 lec 3
Comp102   lec 3Comp102   lec 3
Comp102 lec 3
 
JS BASICS JAVA SCRIPT SCRIPTING
JS BASICS JAVA SCRIPT SCRIPTINGJS BASICS JAVA SCRIPT SCRIPTING
JS BASICS JAVA SCRIPT SCRIPTING
 
Javascript 01 (js)
Javascript 01 (js)Javascript 01 (js)
Javascript 01 (js)
 
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 for C++ programers
Java for C++ programersJava for C++ programers
Java for C++ programers
 
Skills Required For Full Stack Developer.pdf
Skills Required For Full Stack Developer.pdfSkills Required For Full Stack Developer.pdf
Skills Required For Full Stack Developer.pdf
 
Software Engineering 2014
Software Engineering 2014Software Engineering 2014
Software Engineering 2014
 
DOT NET TRaining
DOT NET TRainingDOT NET TRaining
DOT NET TRaining
 
Apache Drill (ver. 0.2)
Apache Drill (ver. 0.2)Apache Drill (ver. 0.2)
Apache Drill (ver. 0.2)
 
C# vs Java What are The Differences.pdf
C# vs Java What are The Differences.pdfC# vs Java What are The Differences.pdf
C# vs Java What are The Differences.pdf
 
INDUSTRIAL TRAINING Presentation on Web Development. (2).pptx
INDUSTRIAL TRAINING Presentation on Web Development. (2).pptxINDUSTRIAL TRAINING Presentation on Web Development. (2).pptx
INDUSTRIAL TRAINING Presentation on Web Development. (2).pptx
 
Demo Lecture 01 Notes.pptx by Sabki Kaksha
Demo Lecture 01 Notes.pptx by Sabki KakshaDemo Lecture 01 Notes.pptx by Sabki Kaksha
Demo Lecture 01 Notes.pptx by Sabki Kaksha
 
Demo Lecture 01 Notes paid , course notes
Demo Lecture 01 Notes paid , course notesDemo Lecture 01 Notes paid , course notes
Demo Lecture 01 Notes paid , course notes
 
Web-Development-ppt (1).pptx
Web-Development-ppt (1).pptxWeb-Development-ppt (1).pptx
Web-Development-ppt (1).pptx
 
Beginners Node.js
Beginners Node.jsBeginners Node.js
Beginners Node.js
 
Product Camp Silicon Valley 2018 - PM Technical Skills
Product Camp Silicon Valley 2018 - PM Technical SkillsProduct Camp Silicon Valley 2018 - PM Technical Skills
Product Camp Silicon Valley 2018 - PM Technical Skills
 
JavaScript New Tutorial Class XI and XII.pptx
JavaScript New Tutorial Class XI and XII.pptxJavaScript New Tutorial Class XI and XII.pptx
JavaScript New Tutorial Class XI and XII.pptx
 

More from Megha V

Soft Computing Techniques_Part 1.pptx
Soft Computing Techniques_Part 1.pptxSoft Computing Techniques_Part 1.pptx
Soft Computing Techniques_Part 1.pptx
Megha V
 
JavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptxJavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptx
Megha V
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
Megha V
 
Python- Regular expression
Python- Regular expressionPython- Regular expression
Python- Regular expression
Megha V
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
Megha V
 
Python programming -Tuple and Set Data type
Python programming -Tuple and Set Data typePython programming -Tuple and Set Data type
Python programming -Tuple and Set Data type
Megha V
 
Python programming –part 7
Python programming –part 7Python programming –part 7
Python programming –part 7
Megha V
 
Python programming Part -6
Python programming Part -6Python programming Part -6
Python programming Part -6
Megha V
 
Python programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsPython programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operations
Megha V
 
Python programming- Part IV(Functions)
Python programming- Part IV(Functions)Python programming- Part IV(Functions)
Python programming- Part IV(Functions)
Megha V
 
Python programming –part 3
Python programming –part 3Python programming –part 3
Python programming –part 3
Megha V
 
Parts of python programming language
Parts of python programming languageParts of python programming language
Parts of python programming language
Megha V
 
Python programming
Python programmingPython programming
Python programming
Megha V
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
Megha V
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrences
Megha V
 
Algorithm Analysis
Algorithm AnalysisAlgorithm Analysis
Algorithm Analysis
Megha V
 
Algorithm analysis and design
Algorithm analysis and designAlgorithm analysis and design
Algorithm analysis and design
Megha V
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
Megha V
 
UGC NET Paper 1 ICT Memory and data
UGC NET Paper 1 ICT Memory and data  UGC NET Paper 1 ICT Memory and data
UGC NET Paper 1 ICT Memory and data
Megha V
 
Seminar presentation on OpenGL
Seminar presentation on OpenGLSeminar presentation on OpenGL
Seminar presentation on OpenGL
Megha V
 

More from Megha V (20)

Soft Computing Techniques_Part 1.pptx
Soft Computing Techniques_Part 1.pptxSoft Computing Techniques_Part 1.pptx
Soft Computing Techniques_Part 1.pptx
 
JavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptxJavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptx
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
 
Python- Regular expression
Python- Regular expressionPython- Regular expression
Python- Regular expression
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Python programming -Tuple and Set Data type
Python programming -Tuple and Set Data typePython programming -Tuple and Set Data type
Python programming -Tuple and Set Data type
 
Python programming –part 7
Python programming –part 7Python programming –part 7
Python programming –part 7
 
Python programming Part -6
Python programming Part -6Python programming Part -6
Python programming Part -6
 
Python programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsPython programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operations
 
Python programming- Part IV(Functions)
Python programming- Part IV(Functions)Python programming- Part IV(Functions)
Python programming- Part IV(Functions)
 
Python programming –part 3
Python programming –part 3Python programming –part 3
Python programming –part 3
 
Parts of python programming language
Parts of python programming languageParts of python programming language
Parts of python programming language
 
Python programming
Python programmingPython programming
Python programming
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrences
 
Algorithm Analysis
Algorithm AnalysisAlgorithm Analysis
Algorithm Analysis
 
Algorithm analysis and design
Algorithm analysis and designAlgorithm analysis and design
Algorithm analysis and design
 
Genetic algorithm
Genetic algorithmGenetic algorithm
Genetic algorithm
 
UGC NET Paper 1 ICT Memory and data
UGC NET Paper 1 ICT Memory and data  UGC NET Paper 1 ICT Memory and data
UGC NET Paper 1 ICT Memory and data
 
Seminar presentation on OpenGL
Seminar presentation on OpenGLSeminar presentation on OpenGL
Seminar presentation on OpenGL
 

Recently uploaded

The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
Fwdays
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 

Recently uploaded (20)

The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 

Introduction to JavaScript

  • 1. JavaScript Megha V Asst. Professor Dept. of IT Kannur University 14-10-2022 1 meghav@kannuruniv.ac.in
  • 2. Introduction to JavaScript • JavaScript is a lightweight, cross-platform, and interpreted compiled programming language which is also known as the scripting language for webpages. • It is well-known for the development of web pages, many non-browser environments also use it. • A scripting language is a programming language that employs a high-level construct to interpret and execute one command at a time. • In general, scripting languages are easier to learn and faster to code in than more structured and compiled languages such as C and C++. 14-10-2022 2 meghav@kannuruniv.ac.in
  • 3. Introduction to JavaScript • JavaScript can be used for Client-side developments as well as Server-side developments. Client-side Programming : • It is the program that runs on the client machine (browser) and deals with the user interface/display and any other processing that can happen on client machine like reading/writing cookies. 1) Interact with temporary storage 2) Make interactive web pages 3) Interact with local storage 4) Sending request for data to server 5) Send request to server 6) work as an interface between server and user 14-10-2022 3 meghav@kannuruniv.ac.in
  • 4. • The Programming languages for client-side programming are : 1) JavaScript 2) VBScript 3) HTML 4) CSS 5) AJAX 14-10-2022 4 meghav@kannuruniv.ac.in
  • 5. Server-side Programming : • It is the program that runs on server dealing with the generation of content of web page. 1) Querying the database 2) Operations over databases 3) Access/Write a file on server. 4) Interact with other servers. 5) Structure web applications. 6) Process user input. • For example if user input is a text in search box, run a search algorithm on data stored on server and send the results. 14-10-2022 5 meghav@kannuruniv.ac.in
  • 6. • Examples : The Programming languages for server-side programming are : 1) PHP 2) C++ 3) Java and JSP 4) Python 5) Ruby on Rails 14-10-2022 6 meghav@kannuruniv.ac.in
  • 7. • Javascript is both imperative and declarative type of language. • JavaScript contains a standard library of objects, like Array, Date, and Math, and a core set of language elements like operators, control structures, and statements. 14-10-2022 7 meghav@kannuruniv.ac.in
  • 8. Client-side: • It supplies objects to control a browser and its Document Object Model (DOM). • When a web page is loaded, the browser creates a Document Object Model of the page. The HTML DOM is a standard object model and programming interface for HTML. It defines: • The HTML elements as objects • The properties of all HTML elements • The methods to access all HTML elements • The events for all HTML elements 14-10-2022 8 meghav@kannuruniv.ac.in
  • 9. • Like if client-side extensions allow an application to place elements on an HTML form and respond to user events such as mouse clicks, form input, and page navigation. • Useful libraries for the client-side are AngularJS, ReactJS, VueJS and so many others. 14-10-2022 meghav@kannuruniv.ac.in 9
  • 10. Server-side: • It supplies objects relevant to running JavaScript on a server. • Like if the server-side extensions allow an application to communicate with a database, and provide continuity of information from one invocation to another of the application, or perform file manipulations on a server. • The useful framework which is the most famous these days is node.js. 14-10-2022 10 meghav@kannuruniv.ac.in
  • 11. Imperative language • In this type of language we are mostly concern about how it is to be done. • It simply control the flow of computation . • The procedural programming approach , object, oriented approach comes under this. 14-10-2022 11 meghav@kannuruniv.ac.in
  • 12. Declarative programming – • In this type of language we are concern about how it is to be done , basically here logical computation require . • Here main goal is to describe the desired result without direct dictation on how to get it like arrow function do. 14-10-2022 12 meghav@kannuruniv.ac.in
  • 13. JavaScript can be added to your HTML file in two ways: • Internal JS: We can add JavaScript directly to our HTML file by writing the code inside the <script> tag. • The <script> tag can either be placed inside the <head> or the <body> tag according to the requirement. • External JS: We can write JavaScript code in other file having an extension .js and then link this file inside the <head> tag of the HTML file in which we want to add this code. 14-10-2022 13 meghav@kannuruniv.ac.in
  • 15. Example: <!DOCTYPE html> <html lang="en"> <head> <title> Basic Example to Describe JavaScript </title> </head> <body> <!-- JavaScript code can be embedded inside head section or body section --> <script> console.log("Welcome to GeeksforGeeks"); </script> </body> </html> 14-10-2022 15 meghav@kannuruniv.ac.in
  • 16. External JS function myFunction() { document.getElementById("demo").innerHTML = "Paragraph changed."; } • External scripts are practical when the same code is used in many different web pages. • JavaScript files have the file extension .js. • To use an external script, put the name of the script file in the src (source) attribute of a <script> tag: <script src="myScript.js"></script> 14-10-2022 meghav@kannuruniv.ac.in 16
  • 17. History of JavaScript: • It was created in 1995 by Brendan Eich while he was an engineer at Netscape. • It was originally going to be named LiveScript but was renamed. • Unlike most programming languages, the JavaScript language has no concept of input or output. • It is designed to run as a scripting language in a host environment, and it is up to the host environment to provide mechanisms for communicating with the outside world. • The most common host environment is the browser. 14-10-2022 17 meghav@kannuruniv.ac.in
  • 18. Features of JavaScript: • According to a recent survey conducted by Stack Overflow, JavaScript is the most popular language on earth. • With advances in browser technology and JavaScript having moved into the server with Node.js and other frameworks, JavaScript is capable of so much more. • Here are a few things that we can do with JavaScript: 14-10-2022 18 meghav@kannuruniv.ac.in
  • 19. • JavaScript was created in the first place for DOM manipulation. • Earlier websites were mostly static, after JS was created dynamic Web sites were made. • Functions in JS are objects. • They may have properties and methods just like another object. • They can be passed as arguments in other functions. • Can handle date and time. • Performs Form Validation although the forms are created using HTML. • No compiler is needed. 14-10-2022 19 meghav@kannuruniv.ac.in
  • 20. Variables and Datatypes in JavaScript Datatypes in JavaScript • There are majorly two types of languages. • First, one is Statically typed language where each variable and expression type is already known at compile time. • Once a variable is declared to be of a certain data type, it cannot hold values of other data types. • Example: C, C++, Java. 14-10-2022 20 meghav@kannuruniv.ac.in
  • 21. // Java(Statically typed) int x = 5 // variable x is of type int and it will not store any other type. string y = 'abc' // type string and will only accept string values Other, Dynamically typed languages: These languages can receive different data types over time. For example- Ruby, Python, JavaScript, etc. // Javascript(Dynamically typed) var x = 5; // can store an integer var name = 'string'; // can also store a string. 14-10-2022 21 meghav@kannuruniv.ac.in
  • 22. • JavaScript is a dynamically typed (also called loosely typed) scripting language. • That is, in JavaScript variables can receive different data types over time. • Datatypes are basically typed data that can be used and manipulated in a program. 14-10-2022 22 meghav@kannuruniv.ac.in
  • 23. • The latest ECMAScript(ES6)-European Computer Manufacturers Association Script standard defines following data types: • Out of which six data types are Primitive(predefined). •Numbers: Represent both integer and floating-point numbers. Example: 5, 6.5, 7 etc. •String: A string is a sequence of characters. In JavaScript, strings can be enclosed within the single or double quotes. Example: “Hello GeeksforGeeks” etc. •Boolean: Represent a logical entity and can have two values: true or false. •Null: This type has only one value : null. •Undefined: A variable that has not been assigned a value is undefined. 14-10-2022 23 meghav@kannuruniv.ac.in
  • 24. • Symbol: Unlike other primitive data types, it does not have any literal form. • It is a built-in object whose constructor returns a symbol-that is unique. • bigint: The bigint type represents the whole numbers that are larger than 253-1. • To form a bigint literal number, you append the letter n at the end of the number. • Object: It is the most important data-type and forms the building blocks for modern JavaScript. 14-10-2022 24 meghav@kannuruniv.ac.in
  • 25. Variables in JavaScript: • Variables in JavaScript are containers that hold reusable data. It is the basic unit of storage in a program. • The value stored in a variable can be changed during program execution. • A variable is only a name given to a memory location, all the operations done on the variable effects that memory location. • In JavaScript, all the variables must be declared before they can be used. 14-10-2022 25 meghav@kannuruniv.ac.in
  • 26. • Before ES2015, JavaScript variables were solely declared using the var keyword followed by the name of the variable and semi-colon. • Below is the syntax to create variables in JavaScript: var var_name; var x; • The var_name is the name of the variable which should be defined by the user and should be unique. • These types of names are also known as identifiers. 14-10-2022 26 meghav@kannuruniv.ac.in
  • 27. • The rules for creating an identifier in JavaScript are, the name of the identifier should not be any pre-defined word(known as keywords), the first character must be a letter, an underscore (_), or a dollar sign ($). • Subsequent characters may be any letter or digit or an underscore or dollar sign. • Notice in the above code sample, we didn’t assign any values to the variables. We are only saying they exist. If you were to look at the value of each variable in the above code sample, it would be undefined. • We can initialize the variables either at the time of declaration or also later when we want to use them. 14-10-2022 27 meghav@kannuruniv.ac.in
  • 28. Below are some examples of declaring and initializing variables in JavaScript: // declaring single variable var name; // declaring multiple variables var name, title, num; // initializing variables var name = "Harsh"; name = “Anu"; 14-10-2022 28 meghav@kannuruniv.ac.in
  • 29. JavaScript is also known as untyped language. This means, that once a variable is created in JavaScript using the keyword var, we can store any type of value in this variable supported by JavaScript. Below is the example for this: // creating variable to store a number var num = 5; // store string in the variable num num = "GeeksforGeeks"; 14-10-2022 29 meghav@kannuruniv.ac.in
  • 30. • The above example executes well without any error in JavaScr unlike other programming languages. • Variables in JavaScript can also evaluate simple mathematical expressions and assume their value. // storing a mathematical expression var x = 5 + 10 + 1; console.log(x); // 16 14-10-2022 30 meghav@kannuruniv.ac.in
  • 31. • After ES2015, we now have two new variable containers: let and const. • The variable type Let shares lots of similarities with var but unlike var, it has scope constraints. • To know more about them visit let vs var. • Let’s make use of the let variable: // let variable let x; // undefined let name = 'Mukul’; // can also declare multiple values let a=1,b=2,c=3; // assignment let a = 3; a = 4; // works same as var 14-10-2022 31 meghav@kannuruniv.ac.in
  • 32. Const is another variable type assigned to data whose value and will not change throughout the script. // const variable const name = 'Mukul’; name = 'Mayank'; // will give Assignment to constant error. 14-10-2022 32 meghav@kannuruniv.ac.in
  • 33. Variable Scope in Javascript • Scope of a variable is the part of the program from where the variable may directly be accessible. In JavaScript, there are two types of scopes: 1.Global Scope – Scope outside the outermost function attached to Window. 2.Local Scope – Inside the function being executed. 14-10-2022 33 meghav@kannuruniv.ac.in
  • 34. JavaScript let globalVar = "This is a global variable"; function fun() { let localVar = "This is a local variable"; console.log(globalVar); console.log(localVar); } fun(); 14-10-2022 34 meghav@kannuruniv.ac.in
  • 36. • When we execute the function fun(), the output shows that both global, and local variables, are accessible inside the function as we are able to console.log them. • This shows that inside the function we have access to both global variables (declared outside the function) and local variables (declared inside the function). • Let’s move the console.log statements outside the function and put them just after calling the function. 14-10-2022 36 meghav@kannuruniv.ac.in
  • 37. JavaScript let globalVar = "This is a global variable"; function fun() { let localVar = "This is a local variable"; } fun(); console.log(globalVar); console.log(localVar); 14-10-2022 37 meghav@kannuruniv.ac.in
  • 39. • We are still able to see the value of the global variable, but for the local variable, console.log throws an error. • This is because now the console.log statements are present in global scope where they have access to global variables but cannot access the local variables. • Also, any variable defined in a function with the same name as a global variable takes precedence over the global variable, shadowing it. 14-10-2022 39 meghav@kannuruniv.ac.in
  • 40. Reference 1. Jeffrey C. Jackson, Web Technologies: A Computer Science Perspective, Prentice Hall 2. David Flanagan, JavaScript: The Definitive Guide, 6th Edn. O'Reilly Media.2011 3. Bob Breedlove, et al, Web Programming Unleashed, Sams Net Publishing, 1stEdn 4. Steven Holzner, PHP: The Complete Reference, McGraw Hill Professional, 2008 5. Steve Suehring, Tim Converse, Joyce Park, PHP6 and MY SQL Bible, John Wiley & Sons,2009 6. Pedro Teixeira, Instant Node.js Starter, Packt Publishing Ltd., 2013 7. Anthony T. HoldenerIII, Ajax: The Definitive Guide, O’Reilly Media, 2008 8. Nirav Mehta, Choosing an Open Source CMS: Beginner's Guide Packt Publishing Ltd, 2009 9. James Snell, Programming Web Services with SOAP, O'Reilly 2002 10. Jacob Lett , Bootstrap Reference Guide, Bootstrap Creative 2018 11. Maximilian Schwarzmüller, Progressive Web Apps (PWA) - The Complete Guide, Packt Publishing 2018 12. Mahesh Panhale, Beginning Hybrid Mobile Application Development, Apress 2016 14-10-2022 40 meghav@kannuruniv.ac.in