Differentiate between local and global variables in JavaScri

.--Differentiate between local and global variables in JavaScript and give a suitable example.                                                                                                                                                                          [1 Mark]

--. Events on a web page can be of different types. Give examples of different events and how JavaScript handles them. Also give an example code of one of the events.

[2Marks]

Solution

Answer 1:

Global variables are declared outside any function, and they can be accessed (used) on any function in the program.

<!DOCTYPE html>

<html>

<body>

<p>A GLOBAL variable can be accessed from any script or function.</p>

<p id=\"demo\"></p>

<script>

var carName = \"Volvo\";

myFunction();

function myFunction() {

    document.getElementById(\"demo\").innerHTML =

    \"I can display \" + carName;

}

</script>

</body>

</html>

output

A GLOBAL variable can be accessed from any script or function.

I can display Volvo

Local variables are declared inside a function, and can be used only inside that function.Since local variables are only recognized inside their functions, variables with the same name can be used in different functions.

Local variables are created when a function starts, and deleted when the function is completed.

<!DOCTYPE html>

<html>

<body>

<p>The local variable carName cannot be accessed from code outside the function:</p>

<p id=\"demo\"></p>

<script>

myFunction();

document.getElementById(\"demo\").innerHTML =

\"The type of carName is \" + typeof carName;

function myFunction() {

    var carName = \"Volvo\";

}

</script>

</body>

</html>

output:

The local variable carName cannot be accessed from code outside the function:

The type of carName is undefined

Answer2:

HTML events are \"things\" that happen to HTML elements.

When JavaScript is used in HTML pages, JavaScript can \"react\" on these events.

Event handlers can be used to handle, and verify, user input, user actions, and browser actions:

Things that should be done every time a page loads

Things that should be done when the page is closed

Action that should be performed when a user clicks a button

Content that should be verified when a user inputs data

A JavaScript can be executed when an event occurs, like when a user clicks on an HTML element.

To execute code when a user clicks on an element, add JavaScript code to an HTML event attribute:

onclick=JavaScript

events:

Examples of HTML events:

Example:

<!DOCTYPE html>

<html>

<body>

<button>Click Me</button>

<p id=\"demo\"></p>

<script>

function myFunction() {

    document.getElementById(\"demo\").innerHTML = \"Hello World\";

}

</script>

</body>

</html>

.--Differentiate between local and global variables in JavaScript and give a suitable example. [1 Mark] --. Events on a web page can be of different types. Give
.--Differentiate between local and global variables in JavaScript and give a suitable example. [1 Mark] --. Events on a web page can be of different types. Give
.--Differentiate between local and global variables in JavaScript and give a suitable example. [1 Mark] --. Events on a web page can be of different types. Give

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site