SlideShare a Scribd company logo
JavaScript language fundamentals
JavaScript is Java code written
in browsers!
JavaScript can be
used to do server
side code as well
JavaScript is
programming
language
JavaScript
make
webpage
look more
beautiful!
JavaScript are
very slow and
unpredictable!
Myths or Facts Let us discover
JavaScript
 Is a scripting language developed by Netscape, and
later standardized by W3C.
 It was originally called LiveScript.
 Used not only for web client and server side web
programs, but also for iPhones, Adobe Photoshop,
Adobe Flash action script to name a few.
 JavaScript on web page works with HTML and CSS
to create a DHTML page.
Our focus
Scripting and Programming language
 Script are line of code that does not execute stand-
alone. They run on browser (client-side) or
application server (server-side) or on top of some
other application.
 They are interpreted at runtime and are not
compiled.
 Usually are loosely-typed language.
 Examples: JavaScript, JScript, VBScript, PHP
Is JScript not
same as
JavaScript?
JScript is
Microsoft version
scripting
language which is
very similar to
JavaScript
Oh! I
thought it
was
VBScript
Microsoft produced 2
scripting languages-
VBScript –(syntax similar
to VB)and JScript
JavaScript in a Web Page
 To create more interactive pages- client side validations
etc.
 To generate html dynamically.
 Event handling
 To enhance browser capabilities by giving it a better
look – printing on status bar etc.
 Interaction with embedded components like applets and
active x controls.
Response
Client
Request
HTML
file
JavaScri
pt
HTML files
JavaScript Execution
Server side code
executes on the
server and sends
the response
Script executes
locally and
interacts with
the browser
Web Server
Server side code
Other
files
So JavaScripts on client side executes faster than server-side code.
Where will the code be written?
 Email format
validation
 Password validation
 Changing the colour
of the page based on
user’s input
 Displaying different
page based on the
user’s role.
 Refreshing just part of
the page
Client-side
JavaScript code
Server-side code
Language Features
 Syntax similar to C++ and Java
 Case sensitive
 Loosely typed
 Interpreted
 Platform independent
 Object-based language
 Semicolon, as separator for multiple statements
in the same line.
Object-based language
 Also called prototype-based object oriented language
 Object-based language
 JavaScript objects are associative arrays
 functions as object constructors and
methods
 prototypes instead
of classes for inheritance.
More on this later
Simple Scripts in HTML
<HTML><HEAD><TITLE>Hello</TITLE></HEAD>
<BODY>
First java script code<br>
<SCRIPT type="text/javascript” >
//Java script single line comment
alert(“Hello java script”);
/* java script script
multi-line comment */
</SCRIPT>
</BODY>
</HTML>
popup
What will happen if you
run this on a browser
that does not support
JavaScript?
NOSCRIPT and Hiding scripts
 Because some browsers don’t support
JavaScript it is wise to put the JavaScript code
inside HTML commented tags.
<SCRIPT type="text/javascript” >
<!—
alert(“Hello java script”)
-->
</SCRIPT>
<NOSCRIPT>
Java script is not supported
</NOSCRIPT>
• The text inside the <NOSCRIPT> tag will not be
processed by the JavaScript aware browsers.
External Script
 Scripts can also be written in a separate file and
can be referenced in a HTML file.
<HTML><HEAD><BODY>
<SCRIPT type="text/javascript”
SRC=“jsfile.js”>
</SCRIPT>
</BODY>
</HTML>
alert(“Hello”);
jsfile.js
Path can be
1. Absolute path
http:://server1/get.jsp
2. Root relative:
/scripts/jsfile.js
3. Document relative
../scripts/jsfile.js
Why would you want
to write JavaScript in
another file?
Placeholders for scripts within HTML
 Inside head
 only declarations. Declarations could for variables or functions.
 No standalone executable statements must appear
 Inside the body
 any statements can appear
 standalone executable statements inside the body are interpreted in
place where it appears.
 Along with the event handler
 Script expression can be written as a value when an event like button
click happens.
Data types
 Data types supported by Java Script are
a) Numeric – integer and floating point numbers
(64 bit, IEE754 floating point)
alert(5+154e-2);
b) String-
alert(“Hello”) ;or
alert(‘Hello’) ;
c) Boolean- true, false
alert(’God gives every bird it's food
but he doesn't throw it into it's
nest’); gives me error
Hey! use escape sequences
alert(’God gives every bird
it's food but he doesn't
throw it into it's nest’);
Yes ! escape sequences can be used or you
could also use double quote for string
literal.
alert(“God gives every bird it's
food but he doesn't throw it
into it's nest”);
Variables and Data types
 Variable names must begin with a letter, under-
score or $, subsequent characters can be a letter,
under-score or $ or number.
 They can be assigned with proper value and used
where ever appropriate . They are called data
stores.
 To declare a variable:
 var x;
 var x=1; declare and initialize
 Variables declaration is not compulsory in
JavaScript. (If you don't declare a variable explicitly,
JavaScript will declare it implicitly for you.)
Example
<HTML><HEAD>
<SCRIPT type="text/javascript”>
$x=false;
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT type="text/javascript”>
alert($x==0);
</SCRIPT>
</BODY>
</HTML>
What do you
think the alert
box will
display?
Find what alert box will
display if $x is uninitialized?
OperatorsArithmetic:
+ - * / % += -= *= /= %= ++
--
Logical:
& | ! && ||
Relational:
> >= < <= == != === !==
String concatenation: +
Bit wise:
>> << >>> >>= <<= >>>=
Ternary: ?:
Conversion functions:
parseInt() and parseFloat()
To convert string to int and float respectively
Examples
S=“abc”;
I=123;
alert(S+I);
B=true;
alert(S+B);
alert(B+1);
alert(5+154e-2 +” kgs”);
alert(“Rs.” +5+154e-2 );
alert("34"==34);
alert(1<".23");
alert(10*"55");
 abc123
 abctrue
 124
6.54 kgs
Rs .51.54
 true
 550
Mixing up data types:
 NaN
 true
 false
 Infinity
 1
 0
 false
alert(10/"abc");
alert(1==true);
alert(1=="true");
alert(5/0);
alert(true & "3");
alert(true & "a");
alert(10/"abc"==NaN);
Then how do we compare
NaNs?
Use isNaN()
No we cannot,
because ‘a’ is a string.
We will look at the
methods of String and
there we will check
out if we can do the
achieve the same.
Shift operators
 The shift left operator looks at the integer to the left of the operator as a 32-bit binary
number.
 >> the SHIFT RIGHT operator
 << the SHIFT LEFT operator
 >>> the UNSIGNED SHIFT RIGHT operator
s=2;
t=-2;
alert((s<<2) +" "+(s>>2)+ " "+(s>>>2));
alert((t<<2)+" "+ (t>>2)+ " "+ (t>>>2));
Result of popup:
8 0 0
-8 -1 1073741823
Find what
happens when
you shift beyond
32 bits?
=== and !==
 alert("34"==34);
 Returns true
 alert("34"===34);
 Returns false
 === and !== are used for stricter comparisons
based on types.
 Note that alert(34.0===34) returns true
 alert(true===1); returns false
Control statements
 Same as in C++ or Java
 if else
 for
 for..in
 while
 do .. while
 switch
Getting input from user
 String prompt(question,defaultanswer)
 Example: prompt(“what is your
name”,””);
 A prompt box pops up. The user can enter some text
and click either "OK" or "Cancel" to proceed after
entering text.
 If the user clicks "OK" the box returns the input
value.
 If the user clicks "Cancel" the box returns null.
Function
 Like other programming languages, in JavaScript
also we can define a reusable code-block which
will execute when ever it is called.
 A function can be defined inside <head> ,
<body> or in a external file and can be called
from anywhere after it has been read.
 Syntax
function functionname(var1,var2,...,varX)
{
//some code
}
Example
<html><head>
<script type="text/javascript”>
<!--
function display(x){
alert(x);}
-->
</script>
</head>
<body>
<script>
<!--
display(“hello”);
-->
</script>
</body></html>
display()can be defined
anywhere even after the
function call
Calling a function
 The above function can be called as
 display();
 display(“hello”);
 Or display with any number of arguments
function display(x){
if(x==null)
x=“Greetings”;
alert(x) ;
}
 You can also pass values to a function that does not take
any arguments!
<body>
<script type="text/javascript”>
display("hello");
display();
function display(x)
{
if(x==null)
x="Greetings";
alert(x) ;
}
function display(){
alert("Greet") ;
}
</script>
</body>
Prints Greet for both the
calls
No overloading possible. If
overloaded functions are
provided, only the last defined
function is considered.
Local and Global variables
 All the variables that are not explicitly declared are global.
 Local variables are created using var inside the function
Global variable
Error.
<html><head>
<script>
total=0;
function sum(){
y=20;
var x=10;
total=x+y;
}
function display(){
sum();
alert(total);
alert(y);
alert(x);
}
Local variable
</script></head><body>
<script>
display();
</script>
</body></html>

More Related Content

PPT
Java script
PPTX
Java Script An Introduction By HWA
PPTX
Introduction to java script
PPTX
Java Script
PPTX
Java script Session No 1
PPTX
Java script
RTF
Java scripts
PPTX
Java script
Java script
Java Script An Introduction By HWA
Introduction to java script
Java Script
Java script Session No 1
Java script
Java scripts
Java script

What's hot (20)

DOC
Basics java scripts
PPT
Java script
PPTX
Javascript
PPT
Java script
PPT
Java Script ppt
DOCX
Javascript tutorial
PPT
Javascript by geetanjali
PPT
Java script -23jan2015
PPTX
Java Script
PPT
Java script final presentation
PPTX
Javascript
PPT
Js ppt
PDF
JavaScript guide 2020 Learn JavaScript
PDF
Javascript
PPTX
Javascript
PPT
Java script Learn Easy
PPTX
Introduction to Java Script
PPTX
Java script
PDF
Basic JavaScript Tutorial
PPT
Learn javascript easy steps
Basics java scripts
Java script
Javascript
Java script
Java Script ppt
Javascript tutorial
Javascript by geetanjali
Java script -23jan2015
Java Script
Java script final presentation
Javascript
Js ppt
JavaScript guide 2020 Learn JavaScript
Javascript
Javascript
Java script Learn Easy
Introduction to Java Script
Java script
Basic JavaScript Tutorial
Learn javascript easy steps
Ad

Similar to 1. java script language fundamentals (20)

PPTX
Unit 3-Javascript.pptx
PDF
javascriptPresentation.pdf
PPTX
Lecture 5 javascript
PDF
Unit 4(it workshop)
PDF
java-scriptcdvcx vnbm,azsdfghjkml;sxdfcgmndxfcgvhb nmfctgvbhjnm ,cfgvb nm,xc ...
PPTX
Final Java-script.pptx
PPTX
MYSQL DATABASE INTRODUCTION TO JAVASCRIPT.pptx
PPTX
JavaScript_III.pptx
PPTX
Java script.pptx v
PPTX
Chapter 3 INTRODUCTION TO JAVASCRIPT S.pptx
PPTX
JavaScript New Tutorial Class XI and XII.pptx
PPTX
Introduction to JAVA SCRIPT USING HTML and CSS
PPTX
Basics of Javascript
PPTX
HNDIT1022 Week 08, 09 10 Theory web .pptx
PPT
Java script
PDF
internet Chapter 4-JavascripPrepare a ppt of video compression techniques bas...
PPTX
Web designing unit 4
PDF
8 introduction to_java_script
PDF
CS8651- Unit 2 - JS.internet programming paper anna university -2017 regulation
Unit 3-Javascript.pptx
javascriptPresentation.pdf
Lecture 5 javascript
Unit 4(it workshop)
java-scriptcdvcx vnbm,azsdfghjkml;sxdfcgmndxfcgvhb nmfctgvbhjnm ,cfgvb nm,xc ...
Final Java-script.pptx
MYSQL DATABASE INTRODUCTION TO JAVASCRIPT.pptx
JavaScript_III.pptx
Java script.pptx v
Chapter 3 INTRODUCTION TO JAVASCRIPT S.pptx
JavaScript New Tutorial Class XI and XII.pptx
Introduction to JAVA SCRIPT USING HTML and CSS
Basics of Javascript
HNDIT1022 Week 08, 09 10 Theory web .pptx
Java script
internet Chapter 4-JavascripPrepare a ppt of video compression techniques bas...
Web designing unit 4
8 introduction to_java_script
CS8651- Unit 2 - JS.internet programming paper anna university -2017 regulation
Ad

More from Rajiv Gupta (18)

PDF
Spring5 hibernate5 security5 lab step by step
PDF
GOF Design pattern with java
PDF
Introduction to jsf2
PDF
Hibernate 3
PDF
Weblogic 11g admin basic with screencast
PDF
Struts2
PDF
jsf2 Notes
PDF
Java 7
PDF
Struts2 notes
PDF
Lab work servlets and jsp
PPT
Java Servlet
PDF
Spring aop with aspect j
PDF
Spring 3.0 dependancy injection
PDF
Java spring framework
TXT
Jsp Notes
PDF
Java Logging discussion Log4j,Slf4j
TXT
Advance C++notes
PDF
Core java 5 days workshop stuff
Spring5 hibernate5 security5 lab step by step
GOF Design pattern with java
Introduction to jsf2
Hibernate 3
Weblogic 11g admin basic with screencast
Struts2
jsf2 Notes
Java 7
Struts2 notes
Lab work servlets and jsp
Java Servlet
Spring aop with aspect j
Spring 3.0 dependancy injection
Java spring framework
Jsp Notes
Java Logging discussion Log4j,Slf4j
Advance C++notes
Core java 5 days workshop stuff

Recently uploaded (20)

PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
Practice Questions on recent development part 1.pptx
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
436813905-LNG-Process-Overview-Short.pptx
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
ETO & MEO Certificate of Competency Questions and Answers
PDF
B.Tech (Electrical Engineering ) 2024 syllabus.pdf
PPTX
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Top 10 read articles In Managing Information Technology.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
PDF
algorithms-16-00088-v2hghjjnjnhhhnnjhj.pdf
PPTX
24AI201_AI_Unit_4 (1).pptx Artificial intelligence
PPTX
Road Safety tips for School Kids by a k maurya.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
AgentX UiPath Community Webinar series - Delhi
PDF
Queuing formulas to evaluate throughputs and servers
Structs to JSON How Go Powers REST APIs.pdf
Practice Questions on recent development part 1.pptx
Lesson 3_Tessellation.pptx finite Mathematics
436813905-LNG-Process-Overview-Short.pptx
Strings in CPP - Strings in C++ are sequences of characters used to store and...
ETO & MEO Certificate of Competency Questions and Answers
B.Tech (Electrical Engineering ) 2024 syllabus.pdf
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
Model Code of Practice - Construction Work - 21102022 .pdf
Top 10 read articles In Managing Information Technology.pdf
bas. eng. economics group 4 presentation 1.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
algorithms-16-00088-v2hghjjnjnhhhnnjhj.pdf
24AI201_AI_Unit_4 (1).pptx Artificial intelligence
Road Safety tips for School Kids by a k maurya.pptx
CH1 Production IntroductoryConcepts.pptx
AgentX UiPath Community Webinar series - Delhi
Queuing formulas to evaluate throughputs and servers

1. java script language fundamentals

  • 2. JavaScript is Java code written in browsers! JavaScript can be used to do server side code as well JavaScript is programming language JavaScript make webpage look more beautiful! JavaScript are very slow and unpredictable! Myths or Facts Let us discover
  • 3. JavaScript  Is a scripting language developed by Netscape, and later standardized by W3C.  It was originally called LiveScript.  Used not only for web client and server side web programs, but also for iPhones, Adobe Photoshop, Adobe Flash action script to name a few.  JavaScript on web page works with HTML and CSS to create a DHTML page. Our focus
  • 4. Scripting and Programming language  Script are line of code that does not execute stand- alone. They run on browser (client-side) or application server (server-side) or on top of some other application.  They are interpreted at runtime and are not compiled.  Usually are loosely-typed language.  Examples: JavaScript, JScript, VBScript, PHP
  • 5. Is JScript not same as JavaScript? JScript is Microsoft version scripting language which is very similar to JavaScript Oh! I thought it was VBScript Microsoft produced 2 scripting languages- VBScript –(syntax similar to VB)and JScript
  • 6. JavaScript in a Web Page  To create more interactive pages- client side validations etc.  To generate html dynamically.  Event handling  To enhance browser capabilities by giving it a better look – printing on status bar etc.  Interaction with embedded components like applets and active x controls.
  • 7. Response Client Request HTML file JavaScri pt HTML files JavaScript Execution Server side code executes on the server and sends the response Script executes locally and interacts with the browser Web Server Server side code Other files So JavaScripts on client side executes faster than server-side code.
  • 8. Where will the code be written?  Email format validation  Password validation  Changing the colour of the page based on user’s input  Displaying different page based on the user’s role.  Refreshing just part of the page Client-side JavaScript code Server-side code
  • 9. Language Features  Syntax similar to C++ and Java  Case sensitive  Loosely typed  Interpreted  Platform independent  Object-based language  Semicolon, as separator for multiple statements in the same line.
  • 10. Object-based language  Also called prototype-based object oriented language  Object-based language  JavaScript objects are associative arrays  functions as object constructors and methods  prototypes instead of classes for inheritance. More on this later
  • 11. Simple Scripts in HTML <HTML><HEAD><TITLE>Hello</TITLE></HEAD> <BODY> First java script code<br> <SCRIPT type="text/javascript” > //Java script single line comment alert(“Hello java script”); /* java script script multi-line comment */ </SCRIPT> </BODY> </HTML> popup What will happen if you run this on a browser that does not support JavaScript?
  • 12. NOSCRIPT and Hiding scripts  Because some browsers don’t support JavaScript it is wise to put the JavaScript code inside HTML commented tags. <SCRIPT type="text/javascript” > <!— alert(“Hello java script”) --> </SCRIPT> <NOSCRIPT> Java script is not supported </NOSCRIPT> • The text inside the <NOSCRIPT> tag will not be processed by the JavaScript aware browsers.
  • 13. External Script  Scripts can also be written in a separate file and can be referenced in a HTML file. <HTML><HEAD><BODY> <SCRIPT type="text/javascript” SRC=“jsfile.js”> </SCRIPT> </BODY> </HTML> alert(“Hello”); jsfile.js Path can be 1. Absolute path http:://server1/get.jsp 2. Root relative: /scripts/jsfile.js 3. Document relative ../scripts/jsfile.js Why would you want to write JavaScript in another file?
  • 14. Placeholders for scripts within HTML  Inside head  only declarations. Declarations could for variables or functions.  No standalone executable statements must appear  Inside the body  any statements can appear  standalone executable statements inside the body are interpreted in place where it appears.  Along with the event handler  Script expression can be written as a value when an event like button click happens.
  • 15. Data types  Data types supported by Java Script are a) Numeric – integer and floating point numbers (64 bit, IEE754 floating point) alert(5+154e-2); b) String- alert(“Hello”) ;or alert(‘Hello’) ; c) Boolean- true, false
  • 16. alert(’God gives every bird it's food but he doesn't throw it into it's nest’); gives me error Hey! use escape sequences alert(’God gives every bird it's food but he doesn't throw it into it's nest’); Yes ! escape sequences can be used or you could also use double quote for string literal. alert(“God gives every bird it's food but he doesn't throw it into it's nest”);
  • 17. Variables and Data types  Variable names must begin with a letter, under- score or $, subsequent characters can be a letter, under-score or $ or number.  They can be assigned with proper value and used where ever appropriate . They are called data stores.  To declare a variable:  var x;  var x=1; declare and initialize  Variables declaration is not compulsory in JavaScript. (If you don't declare a variable explicitly, JavaScript will declare it implicitly for you.)
  • 19. OperatorsArithmetic: + - * / % += -= *= /= %= ++ -- Logical: & | ! && || Relational: > >= < <= == != === !== String concatenation: + Bit wise: >> << >>> >>= <<= >>>= Ternary: ?: Conversion functions: parseInt() and parseFloat() To convert string to int and float respectively
  • 20. Examples S=“abc”; I=123; alert(S+I); B=true; alert(S+B); alert(B+1); alert(5+154e-2 +” kgs”); alert(“Rs.” +5+154e-2 ); alert("34"==34); alert(1<".23"); alert(10*"55");  abc123  abctrue  124 6.54 kgs Rs .51.54  true  550 Mixing up data types:
  • 21.  NaN  true  false  Infinity  1  0  false alert(10/"abc"); alert(1==true); alert(1=="true"); alert(5/0); alert(true & "3"); alert(true & "a"); alert(10/"abc"==NaN); Then how do we compare NaNs? Use isNaN() No we cannot, because ‘a’ is a string. We will look at the methods of String and there we will check out if we can do the achieve the same.
  • 22. Shift operators  The shift left operator looks at the integer to the left of the operator as a 32-bit binary number.  >> the SHIFT RIGHT operator  << the SHIFT LEFT operator  >>> the UNSIGNED SHIFT RIGHT operator s=2; t=-2; alert((s<<2) +" "+(s>>2)+ " "+(s>>>2)); alert((t<<2)+" "+ (t>>2)+ " "+ (t>>>2)); Result of popup: 8 0 0 -8 -1 1073741823 Find what happens when you shift beyond 32 bits?
  • 23. === and !==  alert("34"==34);  Returns true  alert("34"===34);  Returns false  === and !== are used for stricter comparisons based on types.  Note that alert(34.0===34) returns true  alert(true===1); returns false
  • 24. Control statements  Same as in C++ or Java  if else  for  for..in  while  do .. while  switch
  • 25. Getting input from user  String prompt(question,defaultanswer)  Example: prompt(“what is your name”,””);  A prompt box pops up. The user can enter some text and click either "OK" or "Cancel" to proceed after entering text.  If the user clicks "OK" the box returns the input value.  If the user clicks "Cancel" the box returns null.
  • 26. Function  Like other programming languages, in JavaScript also we can define a reusable code-block which will execute when ever it is called.  A function can be defined inside <head> , <body> or in a external file and can be called from anywhere after it has been read.  Syntax function functionname(var1,var2,...,varX) { //some code }
  • 28. Calling a function  The above function can be called as  display();  display(“hello”);  Or display with any number of arguments function display(x){ if(x==null) x=“Greetings”; alert(x) ; }  You can also pass values to a function that does not take any arguments!
  • 29. <body> <script type="text/javascript”> display("hello"); display(); function display(x) { if(x==null) x="Greetings"; alert(x) ; } function display(){ alert("Greet") ; } </script> </body> Prints Greet for both the calls No overloading possible. If overloaded functions are provided, only the last defined function is considered.
  • 30. Local and Global variables  All the variables that are not explicitly declared are global.  Local variables are created using var inside the function Global variable Error. <html><head> <script> total=0; function sum(){ y=20; var x=10; total=x+y; } function display(){ sum(); alert(total); alert(y); alert(x); } Local variable