Open any modern website and you will find things that move, update and respond: menus that open, forms that check themselves, lists that filter as you type. Almost all of this behaviour is written in JavaScript.

The Role of JavaScript

JavaScript is a programming language that runs inside the web browser. A web page has three layers. HTML gives the structure, CSS gives the presentation and JavaScript gives the behaviour. JavaScript can react to a click, read what a user typed, change text or styles, and request data from a server in the background.

Definition: JavaScript. A programming language that runs in the browser and makes web pages interactive.

JavaScript is a different language from Java. The two share part of a name for historical reasons and nothing else. JavaScript follows the ECMAScript standard, and every major browser contains an engine that runs it.

Client-Side Scripting

Code can run in two places. Client-side code runs in the visitor's browser, so it reacts at once and does not need a network trip. Server-side code runs on the web server, and it is used for work that must stay private, such as checking a password or reading a database. This unit and the next concentrate on client-side scripting.

Definition: Client-side scripting. Code that runs in the user's browser instead of on the web server.

Embedding JavaScript in HTML

Code is placed in a <script> element. It can be written between the tags, or kept in a separate file and linked with <script src="app.js"></script>. A separate file is the better choice for the same reason as an external style sheet: many pages can share it. A script that works with page elements must run after those elements exist. It is therefore placed just before the closing </body> tag, or given the defer attribute.

Syntax Basics

A program is a list of statements, and each statement usually ends with a semicolon. JavaScript is case sensitive, so total and Total are different names. A single-line comment starts with //, and a longer one sits between /* and */. The method console.log() prints a value to the browser console, which opens with the F12 key and is the main tool for checking code.

Table 3.1: Ways to include JavaScript in a page

Method Written as Use
Inline in body <script> code </script> Short demonstrations
External file <script src="app.js"> Real projects
Event attribute onclick="..." Avoid in new code

Example 3.1: Your first script

<script>
  const name = "Asha";
  console.log("Hello, " + name + "!");
  console.log(2 + 3);
</script>

Running this code prints two lines in the browser console. The same statements can be tested in Node.js:

const name = "Asha";
console.log("Hello, " + name + "!");
console.log(2 + 3);

Output:

Hello, Asha!
5

Common mistakes

  • Confusing JavaScript with Java, which is a separate language.
  • Placing a script in the head that looks for elements, before the body exists.
  • Forgetting that names are case sensitive, so that Name and name become different variables.
  • Believing that client-side code can keep a secret, when the user can read every line of it.

Key takeaways

  • JavaScript adds behaviour to a page and runs in the browser.
  • HTML gives structure, CSS gives presentation and JavaScript gives behaviour.
  • Link an external script file, and load it after the elements it uses.
  • Use console.log to print values while testing.

Practice questions

  1. Name the three layers of a web page.
  2. Where does client-side code run?
  3. Why is a script often placed at the end of the body?

Answers to the practice questions

  1. HTML for structure, CSS for presentation and JavaScript for behaviour.
  2. In the user's web browser.
  3. So that the page elements exist before the script tries to use them.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit III