0

Computer

Quick Revision

Chapter 4: JavaScript Basics

Key Concepts

  • 1What are the five primitive data types in JavaScript?
  • 2What is the difference between alert(), prompt(), and confirm() in JavaScript?
  • 3What are the three types of loops in JavaScript? When should each be used?

Important Formulas & Facts

#1

1. Number — integers and decimals (e.g., 42, 3.14). 2. String — text in quotes (e.g., 'Hello'). 3. Boolean — true or false. 4. Undefined — variable declared but not assigned. 5. Null — intentional absence of value. (Object and Symbol are also types but non-primitive.)

#2

alert(msg) — displays message in popup with OK button; returns undefined. prompt(msg) — displays message with a text input field; returns entered string or null if cancelled. confirm(msg) — displays message with OK and Cancel buttons; returns true (OK) or false (Cancel).

#3

for loop — use when the number of iterations is known (e.g., iterating an array). while loop — use when the condition is checked before each iteration and count is unknown. do-while loop — use when the body must execute at least once before condition is checked (e.g., menu-driven programs).

Must-Know Questions

Q1Which keyword is used to declare a variable in JavaScript that cannot be reassigned?
Explanation

'const' is used to declare a variable whose value cannot be reassigned once set. 'let' declares a block-scoped variable that can be reassigned. 'var' declares a function-scoped or globally-scoped variable (older style). Example: const PI = 3.14; PI = 3 would throw an error.

Q2Which function in JavaScript is used to display a message in a popup box?
Explanation

alert() displays a popup dialog box with a specified message and an OK button. prompt() displays a dialog box with a message and an input field for user input. confirm() displays a dialog with OK and Cancel buttons. Example: alert('Hello, World!'); will show a popup with the message.

Q3What is the difference between '==' and '===' in JavaScript?
Explanation

'==' is the equality operator that compares values after type conversion (loose equality). Example: 5 == '5' is true because '5' is converted to a number. '===' is the strict equality operator that compares both value AND type without conversion. Example: 5 === '5' is false because number 5 is not the same type as string '5'. It is recommended to use '===' to avoid unexpected results.

Q4Write a JavaScript function to calculate and return the area of a rectangle given its length and breadth.
Explanation

The function is: function calculateArea(length, breadth) { return length * breadth; }. To call it: var area = calculateArea(5, 3); document.write('Area = ' + area); Functions are defined with the 'function' keyword, followed by name, parameters in (), and the function body in {}. The return statement sends a value back to the caller.

Q5Explain the difference between 'var', 'let', and 'const' in JavaScript.
Explanation

var: function-scoped or globally scoped; can be redeclared and updated; hoisted (moved to top of scope during execution). let: block-scoped (within {}); can be updated but not redeclared in same scope; not hoisted in the same way. const: block-scoped; cannot be updated or redeclared; must be initialised at declaration; not hoisted. Best practice: use const by default, use let when reassignment is needed, avoid var in modern JavaScript.

Practice JavaScript Basics

Reinforce what you just revised with practice questions