Outputs in JavaScript (OCR A Level Computer Science)

Revision Note

Becci Peters

Expertise

Computer Science

Outputs in Javascript

There are several ways to produce outputs in JavaScript:

Changing the contents of an HTML element

  • JavaScript can be used to modify the content of HTML elements by accessing them through the Document Object Model (DOM). E.g.:

chosenElement = document.getElementById(“example”);

chosenElement.innerHTML = “Hello World”;

  • The first line of code uses the document.getElementById() method to retrieve the HTML element with the ID "example" from the document's DOM (Document Object Model)

  • The returned element is assigned to the variable chosenElement, which allows further manipulation of that element

  • The second line of code modifies the content within the chosenElement HTML element

  • The innerHTML property allows direct access to the HTML content within an element

  • In this case, the content of chosenElement is changed to "Hello World", effectively replacing any existing content with this new text

Writing directly to the document

  • JavaScript can write directly to the document using the document.write() method. E.g.

document.write(“Hello World”);

  • This code writes the text "Hello World" directly to the webpage

Using an alert box

  • JavaScript provides the alert() function to display a pop-up alert box with a message. E.g.

alert(“Hello World”);

  • This code triggers an alert box with the message "Hello World"

Alert Box with the words 'Hello World'

Alert Box with the words 'Hello World'

You've read 0 of your 0 free revision notes

Get unlimited access

to absolutely everything:

  • Downloadable PDFs
  • Unlimited Revision Notes
  • Topic Questions
  • Past Papers
  • Model Answers
  • Videos (Maths and Science)

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Becci Peters

Author: Becci Peters

Becci has been a passionate Computing teacher for over 9 years, teaching Computing across the UK helping to engage, interest and develop confidence in the subject at all levels. Working as a Head of Department and then as an educational consultant, Becci has advised schools in England, where her role was to support and coach teachers to improve Computing teaching for all. Becci is also a senior examiner for multiple exam boards covering GCSE & A-level. She has worked as a lecturer at a university, lecturing trainee teachers for Computing.