What else can JavaScript be used for?


#1

Question

In the context of this exercise, what are some other things that JavaScript can be used for?

Answer

JavaScript can be used for many useful applications on a web page, mainly by making pages more dynamic and interactive. A few of the most useful things that JavaScript provides is user interaction, and the ability to change the structure and style of a page dynamically.

By utilizing the DOM (Document Object Model) of a page, JavaScript can change the structure of a web page. It can add, remove and change elements on the page dynamically. For example, if we wanted to add a new section to the page, we could do something like

var d = document.createElement("div");
document.body.appendChild(d);

This will create a new <div> element, then add it at the end of the document body.

JavaScript also allows us to change the style of the page, letting us change, add, or remove style rules to elements. For example, if we wanted to set the background color of an element with a specified id, we can do so using JavaScript, like so

document.getElementById("square").style.backgroundColor = "gray";

These are just a few of the many useful features that JavaScript gives us when working with web pages and applications.