7 Javascript Fundamentals

Web Technology I · Unit 7

JavaScript Fundamentals

Exam-focused notes for JavaScript Fundamentals (Web Technology I, BIT251): what the TU syllabus asks and how it has actually been tested, with 4 solved past questions from this unit.

JavaScript Fundamentals in short

JavaScript makes web pages interactive. This chapter covers its basic building blocks: variables, data types, arrays, objects, functions, dialog boxes and strings.

JavaScript Fundamentals: notes

JavaScript variable declaration and definition

A variable is a named place that stores a value. Declaration creates the name. Definition (initialization) gives it a value.

JavaScript has three keywords.

  • var: function scoped, can be redeclared. It is the older style.
  • let: block scoped, can be changed.
  • const: block scoped, cannot be reassigned.
var a;            // declaration
a = 10;           // definition
let name = "Sita";
const PI = 3.14159;
let x = 5, y = 7;

Names may contain letters, digits, _ and $. They cannot start with a digit and are case sensitive. JavaScript is dynamically typed, so one variable can hold different types at different times. A declared variable with no value holds undefined.

JavaScript code is placed in the page with the <script> tag.

<script>
  let msg = "Hello";
  document.write(msg);
</script>

JavaScript data types and values

JavaScript has primitive types and object types.

TypeExample
Number25, 3.14
String"hello", 'hi'
Booleantrue, false
UndefinedA variable with no value
NullAn intentional empty value
Object{name: "Ram"}, arrays, functions
BigInt, SymbolSpecial newer types

The typeof operator tells the type of a value.

console.log(typeof 42);        // "number"
console.log(typeof "text");    // "string"
console.log(typeof true);      // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null);      // "object" (a known quirk)
console.log("5" + 3);          // "53"
console.log("5" - 3);          // 2

JavaScript converts types automatically in some cases. This is called type coercion. The + operator joins strings, while - forces numbers. Use Number("12") and String(12) for clear conversion.

What this unit covers

  • JavaScript variable declaration and definition
  • JavaScript data types and values
  • JavaScript arrays definition and usage
  • JavaScript user defined objects
  • JavaScript functions and methods
  • Prompt and alert dialog boxes
  • String concatenation and manipulation

JavaScript variable declaration and definition

2079 · 5 marks

How variables in JavaScript are defined and declared? Write a JavaScript function that will include the variable having values number 5 and character '5' and print the variables. [5]

JavaScript provides three keywords to declare variables: Keyword Scope Re-declarable Re-assignable Notes ----------------------------------------------------- var Function/Global Yes Yes Old way, hoisted let Block No Yes Modern, preferred const Block No No For constants Note: In JavaScript, varia...

Open this answer on its own page →

JavaScript user defined objects

2080.2 · 5 marks

Create a user-defined object Student in JavaScript. The object should have attributes with values as Name= "Ram", Age=20,DOB=2003/02/03 respectively. Now display the result retrieving the attributes. [5]

Show solved answer

User-Defined Object Student in JavaScript

Code

// Creating a user-defined object 'Student' using object literal notation
var Student = {
    Name: "Ram",
    Age: 20,
    DOB: "2003/02/03"
};

// Displaying / Retrieving the attributes of the Student object
document.write("Student Details: ");
document.write("Name: " + Student.Name + " ");
document.write("Age: " + Student.Age + " ");
document.write("DOB: " + Student.DOB + " ");

Output

Student Details:
Name: Ram
Age: 20
DOB: 2003/02/03

Explanation

StepDescription
Object CreationA user-defined object Student is created using object literal notation { }
AttributesName, Age, and DOB are the properties (attributes) of the object with their respective values
Dot NotationStudent.Name, Student.Age, Student.DOB are used to retrieve the attribute values
displaydocument.write() is used to display the retrieved values on the webpage

Alternative: Using Constructor Function

// Creating Student object using Constructor Function
function Student(Name, Age, DOB) {
    this.Name = Name;
    this.Age = Age;
    this.DOB = DOB;
}

// Creating an instance of Student
var s1 = new Student("Ram", 20, "2003/02/03");

// Retrieving and displaying attributes
document.write("Name: " + s1.Name + " ");
document.write("Age: " + s1.Age + " ");
document.write("DOB: " + s1.DOB + " ");

Note: In JavaScript, objects can be created using object literals, constructor functions, or the class keyword. The object literal method is the simplest and most commonly used approach for creating a single object.

Open this answer on its own page →

Prompt and alert dialog boxes

2080.2 · 5 marks

Write JavaScript program that will take your name as input using prompt and display the name using alert in alert box. [5]

--- Statement Description ------------------------ prompt("...") Displays a dialog box that takes input from the user and returns it as a string var name = prompt(...) Stores the entered name into the variable name alert("Hello, " + name) Displays the name in an alert dialog box using string conc...

Open this answer on its own page →

JavaScript arrays definition and usage

0 · 5 marks

How array is defined in JavaScript? Illustrate with example. [5]

An array in JavaScript is a special type of object used to store multiple values in a single variable. Arrays are ordered collections of data where each element is accessed by its index (starting from 0). JavaScript arrays are dynamic (can grow or shrink in size) and can hold values of different ...

Open this answer on its own page →