0% found this document useful (0 votes)
0 views

JavaScript_Details

The document provides an overview of JavaScript, covering its definition, data types, operators, and key concepts such as the 'this' keyword, callbacks, variable declarations, and error types. It also explains AJAX, event binding, and methods for selecting DOM elements. Overall, it serves as a comprehensive guide to fundamental JavaScript concepts and functionalities.

Uploaded by

rahulharyan2002
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

JavaScript_Details

The document provides an overview of JavaScript, covering its definition, data types, operators, and key concepts such as the 'this' keyword, callbacks, variable declarations, and error types. It also explains AJAX, event binding, and methods for selecting DOM elements. Overall, it serves as a comprehensive guide to fundamental JavaScript concepts and functionalities.

Uploaded by

rahulharyan2002
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

1. **What is JavaScript?

**
JavaScript is a high-level, interpreted programming language primarily used to
create interactive and dynamic content on web pages. It enables developers to build
websites that can respond to user actions, manipulate the DOM, handle events, and
perform complex tasks like animations and API requests.

2. **What are the Different Data Types Present in JavaScript?**


JavaScript has several built-in data types:
- **Primitive Data Types**:
- `Number`: Represents numbers (e.g., `123`, `45.67`).
- `String`: Represents text (e.g., "Hello World").
- `Boolean`: Represents true or false values.
- `Undefined`: Represents a variable that has not been assigned a value.
- `Null`: Represents an intentional absence of any value or object.
- `Symbol`: A unique and immutable primitive value used for property keys.
- **Non-Primitive (Object) Data Types**:
- `Object`: Represents key-value pairs and complex data structures like arrays
and functions.

3. **Difference between “==” and “===” Operators**


- **`==`**: Compares values for equality after type coercion (loose equality).
```javascript
5 == '5' // true
```
- **`===`**: Compares both value and type strictly.
```javascript
5 === '5' // false
```

4. **Explain “this” Keyword**


- **`this`** refers to the object that owns the current function. Its meaning
depends on how it is used:
- In global context: Refers to the global object (`window`).
- In an object method: Refers to the object itself.
- In a constructor function: Refers to the instance of the object being created.

5. **What are Callbacks?**


A **callback** is a function passed as an argument to another function, which is
then invoked inside the outer function. Callbacks are commonly used to handle
asynchronous operations like fetching data or handling events.
- Example:
```javascript
function greet(name, callback) {
console.log('Hello ' + name);
callback();
}
greet('John', function() { console.log('Callback executed!'); });
```

6. **Differences Between Declaring Variables Using var, let, and const**


- **`var`**: Function-scoped or globally scoped, can be re-declared and updated.
```javascript
var x = 5;
```
- **`let`**: Block-scoped, cannot be re-declared but can be updated.
```javascript
let y = 10;
```
- **`const`**: Block-scoped, cannot be re-declared or updated after assignment.
```javascript
const z = 15;
```

7. **What is the Use of isNaN Function?**


The `isNaN` function checks if a value is **Not a Number (NaN)**.
- **Syntax**: `isNaN(value)`
- Returns `true` if the value is NaN; otherwise, `false`.
```javascript
console.log(isNaN('abc')); // true
console.log(isNaN(100)); // false
```

8. **What are Undeclared and Undefined Variables?**


- **Undeclared Variables**: Variables that have not been declared using `var`,
`let`, or `const`.
```javascript
x = 10; // Error: x is not declared
```
- **Undefined Variables**: Variables that have been declared but not assigned a
value.
```javascript
let x;
console.log(x); // undefined
```

9. **What are Global Variables? How are These Variables Declared?**


- **Global Variables**: Variables that are accessible from any part of the script
or program.
- Declared using `var`, `let`, or `const` at the top of the script.
```javascript
let globalVar = 'I am global';
```

10. **What are All the Looping Structures in JavaScript?**


- **For Loop**: Repeats a block of code a specific number of times.
```javascript
for (let i = 0; i < 5; i++) {
console.log(i);
}
```
- **While Loop**: Repeats as long as a condition is true.
```javascript
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
```
- **Do...While Loop**: Executes the block of code at least once before checking the
condition.
```javascript
let i = 0;
do {
console.log(i);
i++;
} while (i < 5);
```
11. **How Can You Convert a String of Any Base to an Integer in JavaScript?**
You can use the `parseInt` function to convert a string to an integer:
- **Syntax**: `parseInt(string, radix)`
- The `radix` specifies the base of the string (e.g., 2 for binary, 10 for
decimal).
```javascript
parseInt('1010', 2); // returns 10
```

12. **What is the Difference Between an Alert Box and a Confirmation Box?**
- **Alert Box**: Displays a message and requires the user to click "OK" to proceed.
Used for displaying simple messages.
```javascript
alert('This is an alert!');
```
- **Confirmation Box**: Displays a message with "OK" and "Cancel" options, allowing
the user to make a choice.
```javascript
confirm('Are you sure?'); // Returns true if OK is clicked, false if Cancel is
clicked.
```

13. **What is Break and Continue Statements?**


- **Break**: Exits the loop prematurely when a condition is met.
```javascript
for (let i = 0; i < 5; i++) {
if (i === 3) break;
console.log(i);
}
```
- **Continue**: Skips the current iteration and proceeds to the next iteration of
the loop.
```javascript
for (let i = 0; i < 5; i++) {
if (i === 3) continue;
console.log(i);
}
```

14. **What are the Different Types of Errors in JavaScript?**


- **Syntax Errors**: Occur when there is a problem with the code syntax.
```javascript
let x = ; // Syntax error
```
- **Reference Errors**: Occur when trying to access a variable that has not been
declared.
```javascript
console.log(y); // Reference error
```
- **Type Errors**: Occur when there is a mismatch between expected and actual data
types.
```javascript
let x = 5 + 'text'; // Type error
```
- **Runtime Errors**: Occur during the execution of the code.
```javascript
let x = 5 / 0; // Infinity, no error but might cause unexpected behavior
```
15. **What is Strict Mode in JavaScript, and How Can It Be Enabled?**
**Strict Mode** is a feature in JavaScript that enforces stricter parsing and
prevents certain types of mistakes. It can be enabled by adding `"use strict";` at
the top of the script.
```javascript
"use strict";
```

16. **What is AJAX?**


**AJAX (Asynchronous JavaScript and XML)** is a set of techniques for making
asynchronous requests to a server, allowing web pages to update parts of themselves
without reloading the entire page. It is commonly used to fetch data from APIs.
- Example:
```javascript
let xhr = new XMLHttpRequest();
xhr.open('GET', 'https://api.example.com/data');
xhr.onload = function() {
if (xhr.status === 200) {
console.log(JSON.parse(xhr.responseText));
}
};
xhr.send();
```

17. **What is Event Binding?**


**Event Binding** refers to the process of attaching event handlers (like `click`,
`mouseover`) to DOM elements dynamically using JavaScript, instead of relying on
inline event handlers.
- Example:
```javascript
document.getElementById('myButton').addEventListener('click', function() {
console.log('Button clicked!');
});
```

18. **How to Select DOM Elements?**


- **By ID**: `document.getElementById('id')` returns the element with the specified
ID.
```javascript
let element = document.getElementById('myElement');
```
- **By Class Name**: `document.getElementsByClassName('class')` returns a
collection of elements with the specified class.
```javascript
let elements = document.getElementsByClassName('myClass');
```
- **By Tag Name**: `document.getElementsByTagName('tag')` returns a collection of
elements with the specified tag name.
```javascript
let elements = document.getElementsByTagName('p');
```
- **Query Selector**: `document.querySelector('selector')` returns the first
matching element.
```javascript
let element = document.querySelector('#myElement');
```
- **Query Selector All**: `document.querySelectorAll('selector')` returns a
NodeList of all matching elements.
```javascript
let elements = document.querySelectorAll('.myClass');
```

You might also like