JavaScript is the programming language of the web. Every modern browser contains a JavaScript engine that runs scripts as part of the page.

What is JavaScript

JavaScript (JS) is a lightweight, interpreted, dynamically typed programming language. It was created by Brendan Eich at Netscape in 1995 in ten days, and is standardised as ECMAScript (ES); ES6 (2015) added let, const, arrow functions, classes and modules. It runs in the browser on the client side, making pages interactive, and also on servers through Node.js. Despite its name, it is unrelated to Java. Its uses include form validation, interactive menus, sliders and games, updating parts of a page, and fetching data from servers.

Definition: JavaScript. A lightweight, interpreted, dynamically typed programming language that runs in web browsers to make pages interactive, and on servers through Node.js.

Adding JavaScript to a Page

JavaScript is written inside a <script> element, either inline in the page or in an external .js file linked with <script src="app.js"></script>. Scripts are usually placed just before </body>, or in the head with the defer attribute, so that the HTML is loaded before the script runs. External files keep code separate and can be cached.

The Output System

JavaScript can produce output in several ways. alert("message") shows a message in a pop-up box with an OK button. document.write("text") writes text or HTML into the page while it loads; if used after loading, it replaces the whole page, so it is used only for simple demonstrations. innerHTML and textContent write into a chosen element, which is the normal way to update a page. console.log() writes to the browser's console, opened with F12, and is used for debugging.

Input Boxes

prompt("question", "default") shows an input box that asks the user for text and returns it as a string, or null if the user presses Cancel. Numbers must be converted with Number() or parseInt(). confirm("question") shows OK and Cancel buttons and returns true or false. These boxes stop the page until the user answers, so modern sites usually use HTML forms instead.

Table 3.1: Output and input methods

Method Purpose Returns
alert() Show a message box Nothing
document.write() Write into the page Nothing
console.log() Write to the console Nothing
prompt() Ask for text String or null
confirm() Ask OK or Cancel true or false

Example 3.1: Greeting with an input box

Problem: Write a script that asks the user's name with prompt, asks for confirmation, and shows a greeting with alert, writing the same greeting in the page and the console.

Solution:

  1. let name = prompt("Your name?", "Student");
  2. if (confirm("Show greeting?")) build the message.
  3. Show it with alert, put it in an element with textContent, and log it with console.log.

Result: The user sees a box, the page and the console all showing "Hello, name!".

<p id="out"></p>
<script>
  let name = prompt("What is your name?", "Student");
  if (name !== null && confirm("Show a greeting?")) {
    const msg = "Hello, " + name + "!";
    alert(msg);                                   // pop-up box
    document.getElementById("out").textContent = msg;  // page
    console.log(msg);                             // console (F12)
  }
</script>

Common mistakes

  • Using document.write after the page has loaded, which erases the page.
  • Treating the value from prompt as a number without converting it.
  • Placing a script in the head without defer, so that it runs before the elements exist.

Key takeaways

  • JavaScript makes pages interactive and runs in every browser.
  • Scripts are inline in <script> or external .js files.
  • Output: alert, document.write, innerHTML and console.log.
  • Input: prompt returns a string; confirm returns true or false.

Practice questions

  1. Who created JavaScript?
  2. What does prompt() return?
  3. Where does console.log() write?

Answers to the practice questions

  1. Brendan Eich, at Netscape, in 1995.
  2. The text entered by the user as a string, or null if Cancel is pressed.
  3. To the browser's developer console.

Sources

  • Official BCA syllabus Web Technologies, course code 25BCA402SEC02, Unit III