Unit 3 of 4 · M.Sc IT Sem 2

Unit 3: JavaScript fundamentals

Web Technologies notes · PTU syllabus (PGCA1909)

3 min read8 topics10 exam questions
On this page
  1. Unit summary
  2. JavaScript in web pages
  3. Client-side and server-side JavaScript
  4. Advantages of JavaScript
  5. Embedding JavaScript in HTML
  6. Input and output in JavaScript
  7. Writing JavaScript: variables, data types and operators
  8. Conditions, switch and loops
  9. Functions
  10. Key terms
  11. Quick revision
  12. Important questions

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
ProcessHow JavaScript runs in a page
  1. 1Browser loads HTML
  2. 2Finds a <script> tag
  3. 3JavaScript engine runs the code
  4. 4Code reads or changes the page (DOM)
  5. 5Responds to events

    Clicks, input

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 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.
3

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.
4

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.
5

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>
6

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.

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

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

  1. Q1.What is JavaScript used for?
  2. Q2.State three advantages of JavaScript.
  3. Q3.How do you link an external JavaScript file?
  4. Q4.Distinguish let and const.
  5. Q5.Write a loop printing 1 to 10.
  6. Q6.What is an arrow function?

Long-answer questions

  1. Q1.Explain the role and advantages of JavaScript.
  2. Q2.Explain the ways of embedding JavaScript in HTML.
  3. Q3.Explain variables, data types and operators in JavaScript.
  4. 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.

WhatsApp us