IP -- M2
IP -- M2
JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for
humans to read and write and easy for machines to parse and generate. It is widely used for
data exchange between a server and a client, especially in web applications.
JSON Syntax:
1. Data is represented as:
Objects: Encapsulated in curly braces {} with key-value pairs.
Arrays: Ordered list of values enclosed in square brackets [].
2. Key-value pairs:
Key: Must be a string and is enclosed in double quotes.
Value: Can be a string, number, boolean, null, array, or another JSON object.
Uses of JSON:
APIs: To send and receive data.
Configuration files: In software and tools (e.g., package.json in Node.js).
Database storage: Some databases like MongoDB use JSON-like documents.
JSON (JavaScript Object Notation) and XML (Extensible Markup Language) are both
used for data representation and transfer, but JSON is often preferred in modern
applications due to several advantages:
Example:
XML: <person><name>John</name><age>30</age></person>
3. Faster Parsing
JSON: Can be natively parsed by most modern programming languages (especially
JavaScript) with built-in functions, leading to faster processing.
XML: Requires additional libraries or parsers, which can slow down processing.
XML: Needs extra steps to convert data into a format that JavaScript can easily work with.
XML: Represents data as a tree of elements, which can make it less intuitive for some use
cases.
7. Better Performance
JSON’s compact format and simpler structure typically result in better performance during
transmission and parsing, especially for applications that deal with a large volume of data.
JSON: Validation schemas (e.g., JSON Schema) are simpler and easier to use.
XML: Uses complex schemas like DTD or XSD, which can be challenging to implement
and maintain.
The Document Object Model (DOM) is a programming interface that allows developers to
interact with and manipulate the structure, content, and style of a web page. It represents an
HTML or XML document as a tree-like structure of objects.
Key Features:
1. Tree Structure:
The document is organized as a tree with nodes representing elements, attributes, or text.
Example:
<html>
<body>
<h1>Hello</h1>
</body>
</html>
DOM Tree:
- html
- body
- h1
- Text: “Hello”
2. Node Types:
3. DOM Manipulation:
Example:
<div id=”myDiv”></div>
<script>
</script>
4. Methods:
5. Event Handling:
The DOM allows you to handle user interactions like clicks or input using:
Element.addEventListener(“click”, function() {
Alert(“Clicked!”);
});
<p id=”text”>Hello</p>
<script>
Function changeText() {
</script>
In JavaScript, built-in objects are predefined objects provided by the language to perform
various tasks, like handling data, performing mathematical calculations, working with
strings, and interacting with the browser. These objects simplify programming by offering
readymade methods and properties to manipulate data and perform operations.
1. Global Objects:
These are fundamental objects available in every JavaScript environment without the need
to import or define them. They provide basic functionalities:
Object: The base for all JavaScript objects, allowing you to create and manipulate custom
objects.
Array: Used to store ordered collections of items (numbers, strings, or other objects).
Number: Represents numerical values and provides methods for number-related tasks.
Symbol: Represents unique and immutable values, often used as object property keys.
BigInt: Used for handling integers larger than the Number type can safely represent.
2. Error Objects:
Date: Allows the creation and manipulation of date and time values.
4. Utility Objects:
5. Browser-Specific Objects:
Available in environments like web browsers, these objects help interact with the web page
and perform browser-related tasks:
Window: Represents the browser window, allowing control over the viewport.
Console: Provides methods like console.log() for debugging and outputting information.
Example Usage:
Console.log(Math.PI); // 3.141592653589793
Console.log(Math.sqrt(16)); // 4
2. Manipulating Strings:
4. Using JSON:
Console.log(jsonString); // ‘{“name”:”Alice”,”age”:25}’
Console.log(parsedObj.name); // “Alice”
Q) Explain how JavaScript can hide HTML Elements with suitable example.
In JavaScript, you can hide HTML elements by modifying their CSS properties. The most
common way to hide an element is by setting its style.display or style.visibility property.
1. Using style.display:
Setting display to “none” completely removes the element from the layout (it doesn’t
occupy any space).
Example:
Document.getElementById(“myElement”).style.display = “none”;
2. Using style.visibility:
Setting visibility to “hidden” hides the element but still reserves its space in the layout.
Example:
Document.getElementById(“myElement”).style.visibility = “hidden”;
3. Using classList:
Example:
Document.getElementById(“myElement”).classList.add(“hidden”);
Syntax
Try {
} catch (error) {
} finally {
Try {
Console.log(undeclaredVariable);
} catch (error) {
} finally {
Console.log(“Execution complete.”);
Output:
Execution complete.
Example 2: Throwing a Custom Error
Function checkAge(age) {
} else {
Console.log(“Access granted.”);
an error
} catch (error) {
} finally {
Output:
Key Points
The catch block receives an Error object that includes details like name (type of error) and
message.
Event handling in JavaScript is the process of detecting and responding to user actions or
browser events, such as clicks, keystrokes, mouse movements, form submissions, etc. It
allows developers to add interactivity to their web applications.
Key Concepts in Event Handling
1. Event: An action or occurrence that happens in the browser (e.g., a button click,
mouse hover, or keypress).
2. Event Listener: A function that “listens” for a specific event on an HTML element
and executes a callback when the event occurs.
3. Event Object: Provides information about the event, such as the type of event, the
target element, and additional data like mouse position or key code.
Button.onclick = function() {
Alert(“Button clicked!”);
};
3. Using addEventListener()
The preferred modern way to attach event listeners. It allows multiple listeners for the same
event and better control.
Button.addEventListener(“click”, () => {
Alert(“Button clicked!”);
});
Example: Button Click Event
<!DOCTYPE html>
<html lang=”en”>
<head>
</head>
<body>
<p id=”message”></p>
<script>
Button.addEventListener(“click”, () => {
});
</script>
</body>
</html>
1. Improved Interactivity: Responds to user actions, making web pages dynamic and
engaging.
2. Separation of Concerns: Using addEventListener() keeps JavaScript separate from
HTML, improving maintainability.
3. Efficient Control: Event delegation allows handling events efficiently, especially in
dynamic content.