About JavaScript
About JavaScript
1
JavaScript
Increased interactivity − You can create interfaces that react when the user
hovers over them with a mouse or activates them via the keyboard.
Richer interfaces − You can use JavaScript to include such items as drag-
and-drop components and sliders to give a Rich Interface to your site visitors.
Immediate feedback to the visitors − They don't have to wait for a page
reload to see if they have forgotten to enter something.
<h1>JavaScript Statements</h1>
<p id="demo"></p>
<script>
var x = 5;
var y = 6;
var z = x + y;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
Syntax:
function name(parameter1, parameter2, parameter3) {
code to be executed
}
Example:
<script>
function myFunction(a, b) {
return a * b;
}
document.getElementById("demo").innerHTML = myFunction(4, 3);
</script>
In JavaScript, scope is the set of variables, objects, and functions you have
access to.
Local variables
Global Variables
Local Variable:
Variables declared within a JavaScript function, become LOCAL to the function.
Example :
Global Variable :
Example :
function myFunction() {
<script>
var cars = ["Saab", "Volvo", "BMW"];
document.getElementById("demo").innerHTML = cars;
</script>
note : The first line (in the script) creates an array named cars
Alternate :
var cars = [
"Saab",
"Volvo",
"BMW"
];
Copyright © 2013 Tech Mahindra. All rights reserved. 13
JavaScript Array Methods