Unit 4 of 4 · BCA Sem 2

Unit 4: Introduction to JavaScript

Web Technologies notes · PTU syllabus (UGSEC2505)

3 min read5 topics10 exam questions
On this page
  1. Unit summary
  2. JavaScript basics: syntax, variables and data types
  3. Conditions, switch and loops
  4. Functions, scope and closures
  5. Objects, strings, arrays, dates, Math and JSON
  6. Events and the forms API
  7. Key terms
  8. Quick revision
  9. Important questions

Unit summary

HTML gives structure and CSS gives style, but JavaScript makes pages interactive — validating forms, reacting to clicks and changing content without reloading. This unit covers JavaScript syntax, variables, data types, operators, conditions, loops, functions, objects, events, strings, arrays, dates, JSON and the forms API.

After this unit you can

  • Write JavaScript with variables, operators, conditions and loops
  • Define and invoke functions and explain scope and closures
  • Use strings, numbers, arrays, dates, Math and JSON
  • Handle events and validate forms

PTU syllabus topics

  • JS syntax
  • statements
  • variables
  • operators
  • data types
  • functions
  • objects
  • events
  • strings and string methods
  • numbers
  • arrays and array methods
  • dates
  • math and random
  • booleans
  • comparisons
  • conditions
  • switch
  • loops
  • type conversion
  • scope
  • JSON
  • forms API
  • function definitions/parameters/invocation/closures
ComparisonHTML vs CSS vs JavaScript
Role
Example

HTML

Structure and content

<h1>, <p>, <form>

CSS

Presentation and layout

color, margin, flexbox

JavaScript

Behaviour and interactivity

Form validation, events

1

Topic 1

JavaScript basics: syntax, variables and data types

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.

2

Topic 2

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

3

Topic 3

Functions, scope and closures

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
4

Topic 4

Objects, strings, arrays, dates, Math and JSON

ComparisonBuilt-in tools
Example
Result

String methods

"SBS College".toUpperCase()

"SBS COLLEGE"

Array methods

[3,1,2].sort()

[1,2,3]

Date

new Date().getFullYear()

Current year

Math

Math.round(Math.random() × 10)

Random 0–10

JSON

JSON.stringify(obj) and JSON.parse(text)

Object ↔ text

  • Objects: let student = { name: "Ana", roll: 12 }; student.name;
  • Array methods: push, pop, shift, slice, splice, indexOf, map, filter, forEach, join.
  • JSON (JavaScript Object Notation) is a text format for exchanging data with servers.
5

Topic 5

Events and the forms API

An event is something that happens on the page: click, change, submit, load, mouseover, keyup. JavaScript can respond to it.

html<form name="login" onsubmit="return validate()">
  <input name="user"> <input type="password" name="pwd">
  <input type="submit" value="Login">
</form>
<script>
function validate() {
  const f = document.forms["login"];
  if (f.user.value === "") { alert("Enter username"); return false; }
  if (f.pwd.value.length < 6) { alert("Password too short"); return false; }
  return true;
}
</script>

Returning false from the submit handler stops the form from being sent.

Key terms

JavaScript
A scripting language that adds interactivity to web pages
Strict equality
=== comparing value and type
Closure
A function that remembers variables from its outer scope
Event
An action on the page that scripts can respond to
JSON
A text format for exchanging data

Quick revision

  • let/const block-scoped; var function-scoped.
  • === checks value and type.
  • Arrow functions: (x) => x × 2.
  • Validate with onsubmit and return false to stop sending.
  • JSON.stringify converts an object to text; JSON.parse converts back.

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.How is JavaScript added to an HTML page?
  2. Q2.Differentiate between == and ===.
  3. Q3.Differentiate between let, const and var.
  4. Q4.What is a closure?
  5. Q5.Name four JavaScript events.
  6. Q6.What is JSON?

Long-answer questions

  1. Q1.Explain the data types, variables and operators of JavaScript with examples.
  2. Q2.Explain functions in JavaScript, including parameters, invocation, scope and closures.
  3. Q3.Write a JavaScript program to validate a registration form.
  4. Q4.Explain arrays and their important methods in JavaScript.

Stuck on this unit?

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

WhatsApp us