Unit 3: Introduction to JavaScript
Web Technologies notes · PTU syllabus (BSIT304)
On this page
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
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
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.
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.
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>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.
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.
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.
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).
Topic 8
Functions
javascriptfunction add(a, b = 0) { return a + b; } // declaration, default parameter
const square = (x) => x * x; // arrow function- Scope:
let/constare block-scoped;varis 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 2Key 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
- Q1.What is client-side JavaScript?
- Q2.Why can't browser JavaScript connect directly to a database?
- Q3.How do you read a text box value in JavaScript?
- Q4.State four advantages of JavaScript.
- Q5.Name three ways to add JavaScript to HTML.
- Q6.Distinguish let and var.
Long-answer questions
- Q1.Explain the role of JavaScript in web pages and its advantages.
- Q2.Explain client-side JavaScript and database connectivity.
- Q3.Write a script to capture user input and display output.
- 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.
