Client-Side Scripting (JavaScript) –
Exam-Oriented Theory Answers
Q1a) Define function and its types with example.
Definition of Function:
In JavaScript, a function is a block of reusable code that performs a specific task.
Functions are used to group code into one unit so it can be called whenever needed.
This promotes code reusability, modularity, and readability.
Functions are declared using the function keyword and can accept parameters and
return values.
Syntax:
function functionName(parameter1, parameter2) {
// code to be executed
}
You can call a function using:
functionName(arg1, arg2);
Types of Functions in JavaScript:
1. Named Function (Regular function):
function greet() {
console.log("Hello, welcome!");
}
greet();
2. Anonymous Function:
var display = function() {
console.log("This is an anonymous function.");
};
display();
3. Arrow Function (ES6):
const add = (a, b) => a + b;
4. Immediately Invoked Function Expression (IIFE):
(function() {
console.log("This is an IIFE function.");
})();
Q1b) Differentiate between concat() and join() methods of array object.
| Feature | concat() Method | join() Method |
|------------------|--------------------------------------------|-------------------------------------------
--|
| Purpose | Combines two or more arrays | Joins all elements of an array
into a string |
| Return Type | Returns a new array | Returns a string |
| Original Array | Does not modify original array | Does not modify original
array |
| Syntax | array1.concat(array2) | array.join(separator) |
| Example | arr1.concat(arr2) | arr.join(", ") |
Q1c) Explain the use of push and pop functions
push():
The push() method is used to add one or more elements at the end of an array.
Example:
let fruits = ['Apple', 'Banana'];
fruits.push('Mango'); // ['Apple', 'Banana', 'Mango']
pop():
The pop() method removes the last element from an array.
Example:
let fruits = ['Apple', 'Banana', 'Mango'];
fruits.pop(); // ['Apple', 'Banana']
Q2a) Write a JavaScript function to insert a string within a string at a
particular position
function insertString(mainStr, insertStr, position) {
return mainStr.slice(0, position) + insertStr + mainStr.slice(position);
}
let result = insertString("Hello World", " Beautiful", 5);
console.log(result); // Output: "Hello Beautiful World"
Q2b) Create a function that takes a string as input, process it and returns
the reversed string.
function reverseString(str) {
return str.split('').reverse().join('');
}
let reversed = reverseString("hello");
console.log(reversed); // Output: "olleh"
Q3a) What is string.
A string in JavaScript is a sequence of characters enclosed in single quotes (''),
double quotes ("") or backticks (``). Strings are used to represent and manipulate
text.
Example:
let name = "John";
let message = 'Welcome to JavaScript';
Q3b) Define array
An array in JavaScript is a special variable that can hold multiple values in a single
variable name.
Example:
let colors = ["Red", "Green", "Blue"];
Q3c) Write a four point difference between group of check-box and group
of radio buttons.
| Feature | Checkboxes | Radio Buttons |
|-----------------------|--------------------------------------------|--------------------------------------
-------|
| Selection | Allows multiple selections | Only one selection at a time
|
| Input Type | <input type="checkbox"> | <input type="radio">
|
| Use Case | For choosing multiple options | For choosing one option
from a group |
| Grouping | Work independently | Grouped using same name
attribute |
Q4a) Give the syntax of "with" statement/clause of javascript with
suitable example.
Syntax:
with (object) {
// statements to be executed
}
Example:
let person = {
name: "John",
age: 30
};
with (person) {
console.log(name); // John
console.log(age); // 30
}
Q5a) Write a javascript to call a function with arguments for subtraction
of two numbers.
function subtract(a, b) {
return a - b;
}
let result = subtract(10, 4);
console.log("Subtraction:", result); // Output: 6
Q5b) Write a javascript function to count the number of Vowels in a given
string.
function countVowels(str) {
let vowels = ['a', 'e', 'i', 'o', 'u'];
let count = 0;
for (let i = 0; i < str.length; i++) {
if (vowels.includes(str[i].toLowerCase())) {
count++;
}
}
return count;
}
let result = countVowels("Education");
console.log("Vowel Count:", result); // Output: 5
Q5d) Write a short note on:
i) Combining Array Elements into a string.
ii) Changing Elements of the Array.
i) Combining Array Elements into a String:
Use join() method.
Example:
let colors = ["Red", "Green", "Blue"];
let result = colors.join(" - "); // "Red - Green - Blue"
ii) Changing Elements of the Array:
Access elements by index.
Example:
let fruits = ["Apple", "Banana", "Mango"];
fruits[1] = "Orange"; // Changes "Banana" to "Orange"
Q6a) How will you define function?
A function in JavaScript is a reusable block of code used to perform a specific task.
Example:
function greet(name) {
console.log("Hello " + name);
}
greet("John"); // Output: Hello John
Q6d) Write a JavaScript that find and displays number of duplicate values
in an array.
function findDuplicates(arr) {
let counts = {};
let duplicates = [];
for (let i = 0; i < arr.length; i++) {
counts[arr[i]] = (counts[arr[i]] || 0) + 1;
}
for (let key in counts) {
if (counts[key] > 1) {
duplicates.push(key);
}
}
console.log("Duplicate values:", duplicates);
console.log("Number of duplicates:", duplicates.length);
}
findDuplicates([1, 2, 3, 2, 3, 4, 5, 1]);
Q7b) State and explain any two properties of array object.
1. length:
Represents the number of elements in the array.
Example:
let colors = ["Red", "Green", "Blue"];
console.log(colors.length); // 3
2. constructor:
Returns the function that created the array's prototype.
Example:
let arr = [1, 2, 3];
console.log(arr.constructor); // function Array()...
Q8a) Write a java script to convert string to number.
let str = "123";
let num1 = Number(str);
let num2 = parseInt(str);
let num3 = +str;
console.log(num1); // 123
console.log(num2); // 123
console.log(num3); // 123
Q8c) Differentiate between call by value and call by reference.
| Feature | Call by Value | Call by Reference |
|------------------|------------------------------------------|----------------------------------------|
| Definition | Passes copy of value | Passes reference/address |
| Data Type | Primitive types | Objects and arrays |
| Original Value | Not affected | Can be changed |
| Example | function(a) { a = 10; } | function(obj) { obj.key = value; } |