6. Intro to Js (1)
6. Intro to Js (1)
2
Web Programming
▪Programming for the World Wide Web involves
▪ Server-side programming
▪ Client-side (browser-side) programming
What is JavaScript?
▪A scripting or programming language that allows you to
implement complex things on web pages.
▪JavaScript is used to program the behaviour of web pages (performing
dynamic tasks).
▪JavaScript are scripts (code) that is executed on the client’s browser
instead of the web-server (Client-side scripts).
▪JavaScript is an implementation of the ECMAScript standard.
▪The JavaScript supported in the browsers typically support additional
objects. (e.g. Window, Frame, Form, DOM, object, etc.)
Three layers of web technologies
5
What Javascript can do?
▪ Create interactive user interface in a web page (e.g. menu, pop-up alert, windows)
▪ Displaying timely content updates
▪ Interactive maps
▪ Animated 2D/3D graphics
▪ Scrolling video jukeboxes
▪ Manipulating web content dynamically:
▪ Change the content and style of an element
▪ Replace images on a page without page reload
▪ Hide/Show contents
▪ Validate Data
▪ Make Calculations
Basic Syntax
▪ JavaScript can be implemented using JavaScript statements that are placed within
the <script>... </script> HTML tags in a web page.
▪ The script tag takes two important attributes –
▪ Language - This attribute specifies what scripting language you are using. Typically, its value will
be javascript. Although recent versions of HTML (and XHTML, its successor) have phased out
the use of this attribute.
▪ Type - This attribute is recommended to indicate the scripting language in use and its value
should be set to "text/javascript".
JavaScript ignores multiple spaces. You can add white space to your script to make it more readable.
var person=“ANJU";
JavaScript Primitive Value types
Value Example
Value Example
https://www.w3schools.com/js/js_operators.asp
Ternary Operator
▪JavaScript includes special operator called ternary operator :? that
assigns a value to a variable based on some condition. This is like a
short form of if-else condition.
Syntax:<condition> ? <value1> : <value2>;
▪Example: Ternary operator
var a = 10, b = 5;
var c = a > b? a : b; // value of c would be 10
var d = a > b? b : a; // value of d would be 5
How to include JavaScript in an HTML file?
1. Anywhere in the html file between <script></script> tags.
Note:
Keeping all code in one place, is always a good habit.
JavaScript Display Possibilities (Output)
• JavaScript can "display" data in different ways:
▪ Writing into an alert box, using window.alert(), window.prompt(),
window.confirm().
JavaScript written
An Event inside HTML
Prompts
Note:
We use comparison and logical operators in if
conditions.
if Statement
• It is used to execute code based on some specific condition.
•The syntax for a basic if statement is as follows −
if (expression)
{ Statement(s) to be executed if the expression is
true }
Example:
if…else Statement
The if...else statement checks the condition and executes code in two
ways:
• If condition is true, the code inside if is executed. And, the code inside else is
skipped.
• If condition is false, the code inside if is skipped. Instead, the code inside else is
executed.
Syntax:
if (expression)
{ Statement(s) to be executed if expression is true }
else
{ Statement(s) to be executed if expression is
false }
Example:
if...else if... statement
• Use else if keyword to check for multiple conditions.
• The syntax for else if statement is: -
if (expression 1)
{ Statement(s) to be executed if expression 1 is true }
else if (expression 2)
else if (expression 3)
while (expression)
{
Statement(s) to be executed if expression is true
}
Example:
do...while Loop
The do...while loop is similar to the while loop except that the condition check happens
at the end of the loop
The syntax for do-while loop in JavaScript is as follows :
do
{
Statement(s) to be executed;
}
while (expression);
Example:
for Loop
If you want to run the same code over and over again, each time with a different
value.
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
Example:
Loop Control
JavaScript provides break and continue statements. These statements are used to
immediately come out of any loop or to start the next iteration of any loop respectively.
The break statement
.
The break statement, which
was briefly introduced with the
switch statement, is used to
exit a loop early, breaking out
of the enclosing curly braces.
The continue Statement
When a continue statement is encountered, the program flow moves to the loop check expression immediately and if the
condition remains true, then it starts the next iteration, otherwise the control comes out of the loop.
<html>
<body>
<script type = "text/javascript">
var x = 1;
document.write("Entering the loop<br /> ");
while (x < 10)
{ x = x + 1;
if (x == 5)
{ continue; // skip rest of the loop body
} document.write( x + "<br />");
}
document.write("Exiting the loop!<br /> ");
</script>
</body>
</html>
Learn from the expert!
• Visit the link below and follow step-by-step:
• https://youtu.be/PkZNo7MFNFg?si=EPyyQKBRMUPA8SDx
{The End}
Q&A