Outputs in JavaScript (OCR A Level Computer Science)

Revision Note

Becci Peters

Written by: Becci Peters

Reviewed by: James Woodhouse

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'

Last updated:

You've read 0 of your 5 free revision notes this week

Sign up now. It’s free!

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

Expertise: Computer Science

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.

James Woodhouse

Author: James Woodhouse

Expertise: Computer Science

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.