0% found this document useful (0 votes)
9 views12 pages

JavaScript Functions and Methods Explained

The document provides detailed explanations and examples of various JavaScript functions, methods, and concepts, including Math functions, conditional statements, object manipulation, and event handling. It also covers HTML elements and their usage in forms, as well as HTTP/HTTPS protocols. Each section includes code snippets to illustrate the concepts effectively.

Uploaded by

H S R
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

JavaScript Functions and Methods Explained

The document provides detailed explanations and examples of various JavaScript functions, methods, and concepts, including Math functions, conditional statements, object manipulation, and event handling. It also covers HTML elements and their usage in forms, as well as HTTP/HTTPS protocols. Each section includes code snippets to illustrate the concepts effectively.

Uploaded by

H S R
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Here are the answers to the questions (88–120) based on the provided cheat sheet and

image.

88. Write short notes on [Link]() and [Link]() functions.


●​ [Link]():
○​ This is a function that returns a random floating-point value1.​

○​ The value returned is in the range of 0 to less than 1 ($0 <= randomNumber < 1$)2.​

○​ Example: [Link]([Link]());3.​

●​ [Link]():
○​ This function always rounds a number up to the next largest integer4.​

○​ It takes a decimal/float number as input and returns an integer.


○​ Example: [Link]([Link](95.90)); // Output: 965.​

89. Explain with examples the difference between loose equality (==)
and strict equality (===) in JavaScript.
●​ Loose Equality (==):
○​ It compares values for equality but does not compare types6.​

○​ It performs type conversion if necessary before comparing.


○​ Example: [Link](2 == "2"); // true7.​

●​ Strict Equality (===):


○​ It compares values for equality including their types8.​

○​ It returns false if the values are the same but types are different.
○​ Example: [Link](2 === "2"); // false9.​

90. What is the use of conditional statements in JavaScript?


●​ Conditional statements allow you to execute a specific block of code only when a specific
condition is true10.​

●​ They help in decision-making within the code logic.


●​ The most common form is the if...else statement11.​
●​ If the initial condition is false, the code can skip to an else if or else block12121212.​

91. Write an example using if...else to check whether a number is even


or odd.
●​ The conditional statement checks if the number is divisible by 2.
●​ Code Example:​
JavaScript​
let number = 10;​
if (number % 2 === 0) {​
[Link]("Even"); // Executed if true​
} else {​
[Link]("Odd"); // Executed if false​
}​


(Note: Syntax based on 13)​

92. Write short notes on any two primitive data types in JavaScript.
●​ Primitive Types are one of the main value categories in JavaScript14.​

●​ Number:
○​ All numbers in JavaScript belong to the Number type15.​

○​ Example: let a = 900;16.​

●​ String:
○​ This represents a stream of characters17.​

○​ Strings must be enclosed with Single quotes, Double quotes, or Backticks18.​

○​ Example: let message = "Hello";19.​

93. Demonstrate the use of typeof() operator with examples.


●​ The typeof() operator is used to find the data type of a specific value or variable20.​

●​ It returns the type as a string (e.g., "number", "string").


●​ Example:​
JavaScript​
let a = 900;​
[Link](typeof a); // Output: number​
let b = "Hello";​
[Link](typeof b); // Output: string​

21

94. What is the role of the getElementById() method?


●​ It is a DOM method used to select a specific HTML element22.​

●​ It looks for an element that matches a specific id attribute provided as an argument23.​

●​ This is often the entry point for manipulating the DOM24.​

●​ Example: [Link]("headingElement");25.​

95. How is the onclick event used to manipulate an element’s content


in JavaScript?
●​ The onclick event occurs when a user clicks on an HTML element26.​

●​ A function name is usually assigned to the HTML onclick attribute to handle the event27.​

●​ Inside the function, you can change content using properties like textContent.
●​ Example:​
JavaScript​
// In HTML: <button onclick="changeText()">Click Me</button>​
function changeText() {​
[Link]("myText").textContent = "New Content";​
}​

28282828

96. Write the steps to declare and assign a variable in JavaScript.


●​ Declaration: Variables are created using the let keyword29.​

○​ Example: let message;30.​


●​ Assignment: Data is put into variables using the assignment operator (=)31.​

○​ Example: message = "Hello Rahul";32.​

●​ Combined: You can do both in one step.


○​ Example: let message = "Hello Rahul";33.​

97. What is object destructuring? Give an example.


●​ Note: This specific topic is not explicitly covered in the provided cheat sheet, but here is
the standard basic answer.
●​ Object destructuring is a syntax that allows you to unpack properties from objects into
distinct variables.
●​ It simplifies extracting data.
●​ Example:​
JavaScript​
let person = { name: "Rahul", age: 25 };​
let { name, age } = person;​
[Link](name); // Output: Rahul​

98. Explain with code how a function can be a value of an object


property.
●​ In JavaScript, object properties can hold functions. These are often called methods.
●​ Example:​
JavaScript​
let person = {​
name: "Rahul",​
greet: function() {​
[Link]("Hello");​
}​
};​
[Link](); // Output: Hello​

(Inferred from general object and function syntax in the PDF).

99. Differentiate between dot notation and bracket notation with


examples.
●​ Dot Notation (.):
○​ Used when you know the exact name of the property.
○​ Standard way to access properties.
○​ Example: [Link]
●​ Bracket Notation ([]):
○​ Used when the property name is stored in a variable or contains special characters
(like spaces).
○​ Example: person["name"]​


(Note: The PDF uses Dot notation for DOM properties like .[Link] 34and Bracket
notation for Arrays 35).​

100. How can you add a new property gender with value "Male" to an
existing object?
●​ You can add a new property by simply assigning a value to a new key on the object.
●​ Syntax: [Link] = value;
●​ Example:​
JavaScript​
let person = { name: "Rahul" };​
[Link] = "Male"; ​
[Link]([Link]); // Output: Male​

101. Write the syntax and example for [Link]() and


appendChild().
●​ createElement():
○​ Creates a new HTML element via JavaScript36.​

○​ Syntax/Example: let h1Element = [Link]("h1");37.​

●​ appendChild():
○​ Appends (adds) an element inside another element38.​

○​ Syntax/Example: [Link](h1Element);39.​

102. Explain how to dynamically add and remove class names in an


element using JavaScript.
●​ Adding a class:
○​ Use the [Link]() method40.​

○​ It provides class names dynamically to an element41.​

○​ Example: [Link]("font-bold");42.​

●​ Removing a class:
○​ Use the [Link]() method43.​

○​ It removes class names dynamically44.​

○​ Example: [Link]("underline");45.​

103. What is the purpose of the splice() method? Explain with one
code example.
●​ The splice() method changes the contents of an array46.​

●​ It can be used for removing, replacing, or adding items47.​

●​ Example (Removing items):​


JavaScript​
let myArray = [1, 2, 3, 4];​
// Syntax: [Link](Start Index, Delete Count)​
[Link](2, 2); ​
// Removes 2 items starting from index 2​

48

104. Explain the working of the join() method with syntax and
example.
●​ The join() method creates and returns a new string49.​

●​ It concatenates (joins) all items in an array, separated by a specified separator50.​

●​ Example:​
JavaScript​
let fruits = ["Apple", "Banana"];​
let joinedString = [Link](", "); ​
// Output: "Apple, Banana"​

51

105. Write a JavaScript example to modify an array element.


●​ Array items are modified by accessing their index and assigning a new value.
●​ Example:​
JavaScript​
let myArray = [5, "six", 2];​
myArray[1] = 6; // Changes "six" to 6​
[Link](myArray); // [5, 6, 2]​

52

106. What is the difference between .push() and .pop() methods?


Illustrate with code.
●​ push():
○​ Adds new items to the end of the array53.​

○​ Example: [Link](true);54.​

●​ pop():
○​ Removes the last item of an array55.​

○​ Returns the item that was removed56.​

○​ Example: let lastItem = [Link]();57.​

107. What is a callback function? Write one example where a function


is passed as an argument.
●​ A callback function is a function that is passed as an argument to another function58.​

●​ Example:​
JavaScript​
function displayGreeting(displayName) {​
displayName(); // Calling the passed function​
}​

displayGreeting(function() {​
[Link]("Rahul");​
});​

59595959

108. Differentiate between passing a function name and a function


expression as a callback.
●​ Function Name:
○​ You define the function separately and pass only its name as the argument.
○​ Example: displayGreeting(displayRahul);60.​

●​ Function Expression:
○​ You assign a function to a variable and pass that variable.
○​ Example:​
JavaScript​
let displayRam = function() { [Link]("Ram"); };​
displayGreeting(displayRam);​

61616161
.​

109. Write code to demonstrate how setTimeout() and clearTimeout()


work together.
●​ setTimeout() schedules a function to run after a delay. clearTimeout() cancels it before it
runs.
●​ Code:​
JavaScript​
let uniqueId = setTimeout(function() {​
[Link]("This will not run");​
}, 1000);​

// Cancel the schedule immediately using the ID​
clearTimeout(uniqueId);​

62626262

110. Write syntax and example of addEventListener() method.
●​ This is the modern approach to handle events in JavaScript63.​

●​ Syntax: [Link](event, function);64.​

●​ Example:​
JavaScript​
let greetBtn = [Link]("greetBtn");​
[Link]("click", function() {​
[Link]("Hi Rahul");​
});​

65

111. Explain the working of AND (&&), OR (||), and NOT (!) operators
with code.
●​ AND (&&): Returns true only if both values are true66.​

○​ Code: true && true is true.


●​ OR (||): Returns true if either value is true67.​

○​ Code: true || false is true.


●​ NOT (!): Returns true if the value is not true (inverts value)68.​

○​ Code: !true is false.

112. What is the purpose of the event object? Explain its type and
target properties with examples.
●​ Purpose: The event object is created by the browser when an event happens to hold
information about that event69.​

●​ [Link]:
○​ Contains the type of event (e.g., "keydown")70.​

○​ Example: [Link]([Link]);71.​

●​ [Link]:
○​ Contains the HTML element that triggered the event72.​
○​ Example: [Link]([Link]);73.​

113. What are Web Resources? Explain different types with examples.
●​ Definition: Web resources are any data that can be obtained via the internet74.​

●​ Types:
○​ HTML: Structure of the page.
○​ CSS: Styling of the page.
○​ JSON: Data format.
○​ Media: Images and Videos.​
* 75

114. Explain the structure of a URL with an example. Identify each


part.
●​ A URL is a text string specifying where a resource is found76.​

●​ Syntax: protocol://domainName/path?query-parameters77.​

●​ Parts:
1.​ Protocol: Standard rules for communication (e.g., HTTP, HTTPS)78.​

2.​ Domain Name: Indicates which web server is requested79.​

3.​ Path: Identifies the specific resource on the server80.​

4.​ Query Parameters: Adds criteria to the request using &81.​

115. What is HTTP? Explain its request and response components.


●​ HTTP (HyperText Transfer Protocol): These are messages sent by the client to initiate
action on a server82.​

●​ Request Components:
○​ Start Line: URL, Method, Version83.​

○​ Headers: Additional info84.​

○​ Body: Data sent to server85.​


●​ Response Components:
○​ Status Line: Version, Status Code, Status Text86.​

○​ Headers: Response info87.​

○​ Body: The resource data requested88.​

116. Explain HTTPS and write two major differences from HTTP.
●​ HTTPS: It is a protocol and part of the standard set of rules for electronic device
communication89.​

●​ Differences (Based on general knowledge as cheat sheet is a summary):


1.​ Security: HTTPS is secure (encrypted), while HTTP is not.
2.​ Data Safety: HTTPS protects data integrity during transfer, HTTP sends data in plain
text.

117. Describe different input element types used in HTML forms and
their applications.
●​ Text: <input type="text"/> - Default input for text90.​

●​ Password: <input type="password"/> - Secure way to enter passwords91.​

●​ Search: <input type="search"/> - Designed for search queries92.​

●​ Radio: <input type="radio"/> - Used to select one option from a list93.​

118. Write short notes on GET and POST methods with syntax
examples.
●​ GET Method:
○​ Used to request resources from a server94.​

○​ Syntax Example: method: "GET" in fetch options95.​

●​ POST Method:
○​ Used to submit data to a server96.​

○​ It includes a body with data.


○​ Syntax Example: method: "POST" in fetch options97.​

119. Explain the working of the preventDefault() method with an


example in a form submission.
●​ The preventDefault() method prevents the browser's default action from happening98.​

●​ In forms, it stops the page from reloading upon submission.


●​ Example:​
JavaScript​
[Link]("submit", function(event) {​
[Link](); // Stops reload​
});​

99

120. Describe the structure and purpose of the <select> and <option>
elements in HTML.
●​ <select> Element:
○​ It is used to create a drop-down list100.​

○​ Structure: <select> ... </select>101.​

●​ <option> Element:
○​ It creates the individual menu items inside the list102.​

○​ The text content is the label, and it usually has a value attribute103.​

○​ Structure: <option value="Active">Active</option>104.​

You might also like