JavaScript Theory Answers
JavaScript Theory Answers
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);
2. Anonymous Function:
var display = function() {
console.log("This is an anonymous function.");
};
display();
pop():
The pop() method removes the last element from an array.
Example:
let fruits = ['Apple', 'Banana', 'Mango'];
fruits.pop(); // ['Apple', 'Banana']
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"
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 |
Example:
let person = {
name: "John",
age: 30
};
with (person) {
console.log(name); // John
console.log(age); // 30
}
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 = [];
2. constructor:
Returns the function that created the array's prototype.
Example:
let arr = [1, 2, 3];
console.log(arr.constructor); // function Array()...
console.log(num1); // 123
console.log(num2); // 123
console.log(num3); // 123