Unit 3: JavaScript fundamentals
Web Technologies notes · PTU syllabus (PGCA1909)
On this page
Unit summary
JavaScript adds behaviour to web pages. This unit covers JavaScript in web pages, its advantages, embedding JavaScript in HTML and writing JavaScript programs.
After this unit you can
- Explain the role of JavaScript in web pages
- State the advantages of JavaScript
- Embed JavaScript in HTML in different ways
- Write programs with variables, operators, conditions, loops and functions
PTU syllabus topics
- JavaScript in web pages
- advantages of JavaScript
- embedding JavaScript in HTML
- writing JavaScript
- 1Browser loads HTML
- 2Finds a <script> tag
- 3JavaScript engine runs the code
- 4Code reads or changes the page (DOM)
- 5Responds to events
Clicks, input
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 and server-side JavaScript
- 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
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 4
Embedding JavaScript in 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 5
Input and output in JavaScript
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 6
Writing JavaScript: variables, data types and operators
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
- JavaScript
- Scripting language run by the browser
- Client-side script
- Code executed on the user's computer
- External script
- JavaScript kept in a separate .js file
- Variable
- Named storage for a value (let, const, var)
- Function
- Reusable block of code
Quick revision
- Role of JavaScript; client vs server side.
- Advantages: interpreted, embedded, simple, fast development, procedural and event-driven.
- Inline, internal, external scripts; prompt, alert, DOM output.
- let, const, var; data types; operators.
- if, switch, for, while, do-while; functions and arrow 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 JavaScript used for?
- Q2.State three advantages of JavaScript.
- Q3.How do you link an external JavaScript file?
- Q4.Distinguish let and const.
- Q5.Write a loop printing 1 to 10.
- Q6.What is an arrow function?
Long-answer questions
- Q1.Explain the role and advantages of JavaScript.
- Q2.Explain the ways of embedding JavaScript in HTML.
- Q3.Explain variables, data types and operators in JavaScript.
- Q4.Explain conditional statements, loops and functions with programs.
Stuck on this unit?
Message SBS on WhatsApp for help with Web Technologies, or to ask about studying M.Sc IT at Synetic.
