JavaScript - session-1
JavaScript - session-1
Contents
✓ JavaScript Introduction
✓ JavaScript Syntax
✓ JavaScript Output
✓ JavaScript Function & Statements
✓ Types of JavaScript
✓ JavaScript Variables
✓ JavaScript Data type
✓ JavaScript Scope
✓ JavaScript Operators 2
JavaScript Introduction
5
What can JavaScript do?
Smart watches
Web application
Website
Games
6
JavaScript Framework
ReactJs Jquery
AngularJs
AngularJs MetorJs
7
Top Websites Built Using JavaScript
8
Benefits Of JavaScript
9
JavaScript Syntax
11
JavaScript Statements
function functionname( )
13
JavaScript Output
14
Using innerHTML
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = ”Hello DCS”;
</script>
</body>
</html> 15
Using document.write()
<!DOCTYPE html>
Example-1: <html>
<body>
<script>
document.write(5 + 6);
</script>
</body>
</html> 16
Using document.write()
<!DOCTYPE html>
<html>
Example-2: <body>
</body>
</html>
<!DOCTYPE html>
<html>
Example-: <body>
<script>
window.alert(5 + 6);
</script>
</body>
</html>
18
Using console.log()
For debugging purposes, you can call the console.log() method in the browser to display data.
<!DOCTYPE html>
Example-:
<html>
<body>
<script>
console.log(5 + 6);
</script>
</body>
</html>
19
Types of JavaScript
20
JavaScript Example : code between the body tag
1.<html>
2.<body>
3.<script type="text/javascript">
4.document.write("JavaScript is a simple language for DCS learners");
5.</script>
6.</body>
7.</html>
box of JavaScript that is contained inside the head tag. ✓ function msg(){
1. alert("Hello JS");
2.}
✓ In this example, we are creating a function msg(). To 3.</script>
4.</head>
create function in JavaScript, you need to write 5.<body>
6.<p>Welcome to JavaScript</p>
function with function_name as given below. 7.<form>
8.<input type="button" value="click"
✓ To call function, you need to work on event. Here we onclick="msg()"/> </form>
9.</body>
are using onclick event to call msg() function. 10.</html>
22
External JavaScript file message.js
1.function msg(){
2. alert("Hello DCS");
3.}
✓ We can create external JavaScript file and
index.html
embed it in many html page.
1.<html>
✓ It provides code re usability because 2.<head>
3.<script type="text/javascript" src="message.js"><
single JavaScript file can be used in /script>
4.</head>
several html pages. 5.<body>
6.<p>Welcome to JavaScript</p>
✓ An external JavaScript file must be saved 7.<form>
8.<input type="button" value="click" onclick="msg(
by .js extension. Let's create an )"/>
9.</form>
external JavaScript.
10.</body> 23
11.</html>
JavaScript Variables
✓ There are two types of variables in JavaScript : local variable and global variable.
Memory Location
Variable Name 24
JavaScript Variables Cont’d
25
JavaScript Variables Cont’d
✓ var,
✓ const,
✓ and let.
26
JavaScript var keyword
✓ The var keyword is used in all JavaScript code from 1995 to 2015.
✓ In this example, x, y, and z, are variables, declared with the var keyword:
27
JavaScript const keyword
✓ JavaScript const variables must be assigned a value when they are declared:
28
JavaScript Let keyword
✓ let keyword was introduced later by the ES2015/ES6, which create block
scope variable.
let variable_name;
29
JavaScript Data types
30
JavaScript Data types
JavaScript Strings
✓ A string is a variable which stores a series of characters like "John Doe". A string
can be any text inside quotes. You can use single or double quotes:
31
JavaScript Data types
JavaScript Numbers
✓ JavaScript has only one type of numbers. Numbers can be written with, or
without decimals:
✓ Example:
32
JavaScript Data types
JavaScript Booleans
✓ Example:
33
JavaScript Data types
JavaScript Array
✓ An array is a special variable, which can hold more than one value at a
time.
34
JavaScript Data types
1. Regular:
35
JavaScript Data types
1. Condensed:
36
JavaScript Data types
1. Literal:
37
JavaScript Data types
JavaScript Object
✓ An object is delimited by curly braces. Inside the braces the object's properties are
defined as name and value pairs (name : value). The properties are separated by
commas.
38
JavaScript Scope
39
JavaScript global variable
For example:
✓ A JavaScript global
variable is accessible from
any function.
✓ A variable i.e. declared
outside the function or
declared with window
object is known as global
variable.
40
JavaScript local variable
For example:
✓ A JavaScript local variable is
declared inside block or function.
✓ It is accessible within the function or
block only.
41
JavaScript Arithmetic Operators
++ Increment a= ++b
-- Decrement a= b-- 42
Logical operators
variables.
44
Conditional Operators
✓ The conditional operator is the only JavaScript operator that takes three
operands. The operator can have one of two values based on a condition.
✓ Syntax:
var variable_name=(condition)?value1:value2
45
Conditional Operators Example
46
+ Operator used on Strings
✓ The + operator can be used to add string variables or text values together or it can be used to add a
string to a number.
Example:
47
delete operator in JavaScript
✓ The delete operator deletes an object, an object's property, or an element at a specified index in an
array.
delete objectName;
delete objectName.property;
delete objectName[index];
where objectName is the name of an object, property is an existing property, and index is
an integer representing the location of an element in an array.
48
delete operator in JavaScript
x = 42;
var y = 43;
✓ When you delete an array element, the array length is not affected. For example, if you
delete a[3], a[4] is still a[4] and a[3] is undefined.
✓ The trees[3] is removed with delete. However, trees[3] is still addressable and
returns undefined.
50
delete operator in JavaScript
✓ If you want an array element to exist but have an undefined value, use
the undefined keyword instead of the delete operator.
51
First JavaScript Program
52
JavaScript Tutorial
53
Question & Answer
Thank
You !!