Unit 3 of 4 · B.Sc IT Sem 3

Unit 3: Introduction to JavaScript

Web Technologies notes · PTU syllabus (BSIT304)

3 min read8 topics10 exam questions
On this page
  1. Unit summary
  2. JavaScript in web pages
  3. Client-side JavaScript and database connectivity
  4. Capturing user input
  5. Advantages of JavaScript
  6. Writing JavaScript into HTML
  7. JavaScript basics
  8. Conditions, switch and loops
  9. Functions
  10. Key terms
  11. Quick revision
  12. Important questions

Unit summary

JavaScript makes web pages interactive. This unit covers JavaScript in web pages, database connectivity, client-side JavaScript, capturing user input, the advantages of JavaScript — interpreted, embedded, minimal syntax, quick development and procedural capabilities — and writing JavaScript into HTML.

After this unit you can

  • Explain the role of JavaScript in web pages
  • Explain client-side JavaScript and database connectivity
  • Capture user input
  • Write JavaScript into HTML

PTU syllabus topics

  • JavaScript in web pages
  • database connectivity
  • client-side JavaScript
  • capturing user input
  • advantages of JavaScript (interpreted, embedded, minimal syntax, quick development, procedural capabilities)
  • writing JavaScript into HTML
ComparisonClient-side vs server-side scripting
Client-side
Server-side

Runs on

The browser

The web server

Languages

JavaScript

PHP, Node.js, Python, Java

Uses

Validation, interactivity

Database access, business logic

Source visible?

Yes

No

1

Topic 1

JavaScript in web pages

  • JavaScript: a scripting language run by the browser to add interactivity — validating forms, responding to events, changing page content (DOM), animations, fetching data.
2

Topic 2

Client-side JavaScript and database connectivity

  • Client-side JavaScript runs in the browser and cannot access a server database directly for security.
  • Database connectivity: JavaScript sends requests (fetch, AJAX) to a server-side program (PHP, Node.js, Java) which queries the database and returns data (often JSON); server-side JavaScript (Node.js) can connect to databases directly.
3

Topic 3

Capturing user input

html<input id="nm" placeholder="Your name">
<button onclick="greet()">Greet</button>
<p id="out"></p>
<script>
  function greet() {
    const name = document.getElementById("nm").value;   // read input
    document.getElementById("out").textContent = "Hello, " + name;
  }
  // Dialog boxes also capture input:
  // const age = prompt("Enter age"); alert("Age: " + age);
</script>
4

Topic 4

Advantages of JavaScript

  • Interpreted: no compilation; runs directly in the browser.
  • Embedded: written inside HTML or linked files.
  • Minimal syntax: easy to learn.
  • Quick development: immediate feedback in the browser.
  • Procedural capabilities: conditions, loops, functions — plus objects and events.
  • Others: reduces server load, rich interfaces, runs on all browsers.
5

Topic 5

Writing JavaScript into HTML

  • Inline: in event attributes — button onclick="...".
  • Internal: inside a script element in head or body.
  • External: in a .js file linked with script src="app.js" — preferred for reuse and caching.
6

Topic 6

JavaScript basics

JavaScript runs in the browser. It is added with <script> tags, either inline or from a file: <script src="app.js"></script>.

  • Variables: let (can change), const (cannot be reassigned), var (old, function-scoped).
  • Data types: number, string, boolean, undefined, null, object (including arrays) and symbol/bigint.
  • Type conversion: Number("5") → 5, String(5) → "5", parseInt("12px") → 12.
  • Comparisons: == compares value after conversion ("5" == 5 is true); === compares value and type ("5" === 5 is false).

Exam tip

Always prefer === in answers and code — it avoids surprising conversions.

7

Topic 7

Conditions, switch and loops

javascriptlet marks = 72;
if (marks >= 75) grade = "A";
else if (marks >= 60) grade = "B";
else grade = "C";

for (let i = 1; i <= 5; i++) console.log(i);
let n = 0;
while (n < 3) n++;

switch works like in C; loops include for, while, do…while, for…of (values of an array) and for…in (keys of an object).

8

Topic 8

Functions

javascriptfunction add(a, b = 0) { return a + b; }    // declaration, default parameter
const square = (x) => x * x;                // arrow function
  • Scope: let/const are block-scoped; var is function-scoped; variables outside any function are global.
  • Closure: an inner function remembers the variables of the outer function even after the outer function has finished.
javascriptfunction counter() {
  let count = 0;
  return () => ++count;
}
const next = counter();
next(); next();   // 1, then 2

Key terms

Client-side script
Code executed in the browser
DOM
Document object model representing the page
AJAX
Asynchronous requests to the server without reloading
Event handler
Code run when an event occurs
External script
JavaScript in a separate .js file

Quick revision

  • Role of JavaScript; client vs server side.
  • Database access through server APIs.
  • Input via forms, prompt; output via DOM, alert.
  • Advantages; inline, internal, external scripts.
  • Variables, data types, operators, conditions, loops, functions.

Important exam questions

Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).

Short-answer questions

  1. Q1.What is client-side JavaScript?
  2. Q2.Why can't browser JavaScript connect directly to a database?
  3. Q3.How do you read a text box value in JavaScript?
  4. Q4.State four advantages of JavaScript.
  5. Q5.Name three ways to add JavaScript to HTML.
  6. Q6.Distinguish let and var.

Long-answer questions

  1. Q1.Explain the role of JavaScript in web pages and its advantages.
  2. Q2.Explain client-side JavaScript and database connectivity.
  3. Q3.Write a script to capture user input and display output.
  4. Q4.Explain variables, conditions, loops and functions in JavaScript.

Stuck on this unit?

Message SBS on WhatsApp for help with Web Technologies, or to ask about studying B.Sc IT at Synetic.

WhatsApp us