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.
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 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 answerHide 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
| Step | Description |
|---|---|
| Object Creation | A user-defined object Student is created using object literal notation { } |
| Attributes | Name, Age, and DOB are the properties (attributes) of the object with their respective values |
| Dot Notation | Student.Name, Student.Age, Student.DOB are used to retrieve the attribute values |
| display | document.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
classkeyword. The object literal method is the simplest and most commonly used approach for creating a single object.
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]
JavaScript arrays definition and usage
0 · 5 marks
How array is defined in JavaScript? Illustrate with example. [5]
Make Unit 7 stick
Practice BIT251 with flashcards & quizzes