Internet of everything
Internet of everything

Start this free course now. Just create an account and sign in. Enrol and complete the course for a free statement of participation or digital badge if available.

3.3.5 JavaScript programming example

To give you a better understanding of computer programs, it is useful to analyse some JavaScript code.

JavaScript is a scripting language used primarily in web applications. For example, consider a fictional web application called Cisco Coffee. This application is designed to act as a monitoring tool, or dashboard, for a coffee farm. Figure 18 shows a coffee farm.

In this scenario, many sensors are installed in various locations in the coffee field, close to the coffee bean plants. These sensors report data back to a central station. This station uses the Cisco Coffee web interface to allow users to monitor the field.

Three types of sensors are installed: temperature, sunlight, and soil moisture. If the temperature drops below 77°F, a warning is presented on the interface screen. If the coffee plants are exposed to too much sunlight, a different warning is presented. If the soil becomes too wet or too dry, a different warning is shown.

The following JavaScript snippets are used to implement these tests.

if (temp < 77) {

document.getElementById("logArea").innerHTML =

"WARNING: Field temperature dropped below 77F.";

If the temperature drops below 77 degrees, then trigger an alert.

if (sun > 17000) {

document.getElementById("logArea").innerHTML =

"WARNING: There's too much sunlight on the coffee plants.";


If the sunlight is greater than 17000 lux, then trigger an alert.

if (if ((moist &lt; 5) || (moist > 20)) {

document.getElementById("logArea").innerHTML =

"WARNING: Field moisture level is out of the optimal range."


If the moisture is less than 5 or greater than 20, then trigger an alert.

Warnings provide an opportunity for feedback loops. For example, if the soil moisture level is low, it might be necessary to activate the irrigation system and alert the farmer who may be aware of other circumstances and can make an appropriate decision. The farmer might decide to intervene and turn off the irrigation system because rain is in the forecast. Regardless of how the irrigation occurs, the sensor reporting soil moisture should begin showing more desirable levels, completing the feedback loop.

Take your learning further

Making the decision to study can be a big step, which is why you'll want a trusted University. The Open University has 50 years’ experience delivering flexible learning and 170,000 students are studying with us right now. Take a look at all Open University courses.

If you are new to University-level study, we offer two introductory routes to our qualifications. You could either choose to start with an Access module, or a module which allows you to count your previous learning towards an Open University qualification. Read our guide on Where to take your learning next for more information.

Not ready for formal University study? Then browse over 1000 free courses on OpenLearn and sign up to our newsletter to hear about new free courses as they are released.

Every year, thousands of students decide to study with The Open University. With over 120 qualifications, we’ve got the right course for you.

Request an Open University prospectus371