0% found this document useful (0 votes)
3 views

Javascript session 2

Uploaded by

John
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Javascript session 2

Uploaded by

John
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Internet and Web

Programming
MS.PRATHYAKSHINI
ISE DPT
NMAMIT,NITTE
JavaScript Variables

 JavaScript Variables can be declared in 4 ways:

1. Automatically
2. Using var
3. Using let
4. Using const
<script>
x = 5;
y = 6;
z = x + y;
document.getElementById("demo").innerHTML =
"The value of z is: " + z;
</script>

 In this first example, x, y, and z are undeclared variables.

 They are automatically declared when first used:


 1. Always declare variables

 2. Always use const if the value should not be changed

 3. Always use const if the type should not be changed (Arrays


and Objects)

 4. Only use let if you can't use const

 5. Only use var if you MUST support old browsers.


 You declare a JavaScript variable with the var or the let keyword:

 var carName;
 or:
 let carName;

 To assign a value to the variable, use the equal sign:


 carName = "Volvo";
 You can also assign a value to the variable when you declare it:
 let carName = "Volvo";
If you re-declare a JavaScript variable declared with var, it will not lose its value.

 let person = "John Doe", carName = "Volvo", price = 200;

 let person = "John Doe",


carName = "Volvo",
price = 200;
 You cannot re-declare a variable declared with let or const.

 You can redeclare variable using var, it will not lose its value
JavaScript variables

 JavaScript uses the keywords var, let and const to declare


variables.

<script>
let x;
x = 6;
document.getElementById("demo").innerHTML = x;
</script>
JavaScript functions

 A JavaScript function is defined with the function keyword, followed by a name, followed
by parentheses ().

 Function names can contain letters, digits, underscores, and dollar signs (same rules as
variables).

 The parentheses may include parameter names separated by commas:


 (parameter1, parameter2, ...)

 The code to be executed, by the function, is placed inside curly brackets: {}

function name(parameter1, parameter2, parameter3) {


// code to be executed
}
JavaScript functions

<script>
function myFunction(p1, p2) {
return p1 * p2;
}

let result = myFunction(4, 3);


document.getElementById("demo").innerHTML = result;
</script>
Function Invocation

 When an event occurs (when a user clicks a button)


 When it is invoked (called) from JavaScript code
 Automatically (self invoked)
Function return

 When JavaScript reaches a return statement, the function will


stop executing.

 If the function was invoked from a statement, JavaScript will


"return" to execute the code after the invoking statement.
// Function is called, the return value will end up in x
let x = myFunction(4, 3);

function myFunction(a, b) {
// Function returns the product of a and b
return a * b;
}
Event Handling

You might also like