JavaScript - Basic Flashcards

1
Q

What is JavaScript?

A

JavaScript is a high-level, interpreted programming language primarily used for developing dynamic web applications. It is a scripting language that allows developers to add interactivity and functionality to web pages. JavaScript can be executed on the client-side, within the user’s web browser, or on the server-side using technologies such as Node.js.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the key features of JavaScript?

A

Dynamic typing

Prototypal inheritance

First-class functions

Event-driven programming

Support for manipulating and traversing the Document Object Model (DOM)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Dynamic typing

A

JavaScript variables are not bound to specific data types and can hold values of any type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Prototypal inheritance

A

Objects in JavaScript can inherit properties and methods from other objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

First-class functions

A

Functions in JavaScript are treated as first-class citizens, meaning they can be assigned to variables, passed as arguments to other functions, and returned as values from functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Event-driven programming

A

JavaScript allows handling events and executing code in response to user actions or system events.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Support for manipulating and traversing the Document Object Model (DOM)

A

JavaScript provides APIs to interact with HTML and XML documents, allowing dynamic modification of their content and structure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the differences between JavaScript and Java?

A

Java is a statically-typed language, while JavaScript is dynamically typed.

JavaScript is primarily used for client-side web development, whereas Java is used for a wide range of applications, including web development, server-side development, mobile apps, and enterprise software.

Java code is compiled into bytecode and executed on a virtual machine (JVM), whereas JavaScript code is interpreted and executed by the web browser.

Java has a strong object-oriented programming (OOP) focus, while JavaScript supports both OOP and functional programming paradigms.

Java has a larger standard library compared to JavaScript.

Java is a compiled language, requiring a separate compilation step, while JavaScript is typically interpreted at runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you include JavaScript code in an HTML file?

A

Inline

Internal

External

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you include JavaScript in HTML inline?

A

JavaScript code can be directly placed within the

 tags within the HTML file.
<script>
// JavaScript code goes here
</script>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you include JavaScript in HTML internally?

A

JavaScript code can be placed within the

 tags in the <head> or <body> section of the HTML file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you include JavaScript in HTML internally?

A

JavaScript code can be written in a separate .js file and included in the HTML file using the

 tag with the src attribute pointing to the JavaScript file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the different data types in JavaScript?

A

Primitive types

Complex types

Additionally, ES6 introduced the symbol type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Examples of primitive types?

A

string, number, boolean, null, and undefined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Example of complex types?

A

object (including arrays and functions).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Explain the concept of hoisting in JavaScript.

A

Hoisting is a JavaScript behavior where variable and function declarations are moved to the top of their containing scope during the compilation phase, before the code is executed. This means that you can use variables and call functions before they are declared in the code, without encountering errors. However, only the declarations are hoisted, not the initializations or assignments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are the different ways to define variables in JavaScript?

A

var

let

const

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

define var

A

This was the traditional way of declaring variables in JavaScript prior to the introduction of ES6. Variables declared with var are function-scoped or globally scoped.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

define let

A

Introduced in ES6, ‘let’ allows block-scoped variable declarations. Variables declared with let are limited to the block scope in which they are defined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

define const

A

Also introduced in ES6, ‘const’ is used to declare variables that are block-scoped and whose values cannot be re-assigned once defined.

However, ‘const’ does not make objects immutable; it only prevents re-assignment of the variable itself.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How do you check the data type of a variable in JavaScript?

A

You can use the typeof operator to check the data type of a variable. It returns a string indicating the type of the operand. For example:

typeof variableName;

This will return a string representing the type of the variableName.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Explain the difference between null and undefined in JavaScript.

A

null is an assignment value that represents the intentional absence of any object value. It is a primitive value that can be assigned to a variable to indicate the absence of an object.

undefined is a built-in value that represents an uninitialized, missing, or unknown value. It is the default value assigned to variables that have been declared but not initialized or assigned a value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What are the arithmetic operators in JavaScript?

A

Addition: +
Subtraction: -
Multiplication: *
Division: /
Remainder (Modulus): %
Increment: ++
Decrement: –
Exponentiation: **

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the purpose of the “this” keyword in JavaScript?

A

The this keyword in JavaScript refers to the current object or context within which a function is being executed.

It allows access to properties and methods of the object.

The value of this depends on the invocation context of the function and can be determined using several rules, such as the way the function is called (as a method, constructor, or standalone function) or by using explicit binding methods like call() or apply().

25
Q

Explain the concept of closures in JavaScript?

A

A closure is a combination of a function and the lexical environment within which that function was declared.

It allows a function to access variables from its outer scope, even after the outer function has finished executing.

Closures are created when a nested function is defined within another function and the inner function references variables from the outer function.

Closures are often used to create private variables and to implement data hiding and encapsulation in JavaScript.

26
Q

What is event delegation in JavaScript?

A

Event delegation is a technique in JavaScript where you attach a single event listener to a parent element instead of attaching individual event listeners to multiple child elements.

This approach leverages the event bubbling mechanism in which events triggered on child elements “bubble up” through their ancestors.

By handling events at a higher level in the DOM tree, you can efficiently manage events for dynamically added or removed child elements and reduce the memory footprint by avoiding excessive event listeners.

27
Q

How does prototypal inheritance work in JavaScript?

A

JavaScript uses prototypal inheritance, where objects can inherit properties and methods from other objects.

Each object in JavaScript has an internal prototype property, which points to another object.

When you access a property or method on an object, JavaScript first looks for that property or method on the object itself. If it doesn’t find it, it follows the prototype chain by looking at the object’s prototype object, and so on, until the property or method is found or until the end of the prototype chain is reached (with null as the prototype).

28
Q
  • What is the purpose of the “use strict” directive in JavaScript?
A

The “use strict” directive is a feature introduced in ECMAScript 5 (ES5) that enables strict mode in JavaScript.

When this directive is placed at the beginning of a script or a function, it enables a stricter set of rules and prevents the use of certain error-prone language features.

Strict mode helps in writing more reliable and maintainable JavaScript code by catching common mistakes and turning them into errors.

29
Q

How do you create an object in JavaScript?

A

Object literal notation

Constructor function

ES6 classes

Object.create() method

30
Q

What’s object literal notation?

A

const obj = { key: value };

31
Q

What’s a constructor function

A

function Person(name) {
this.name = name;
}

const person = new Person(‘John’);

32
Q

What’s an ES6 class?

A

class Person {
constructor(name) {
this.name = name;
}
}

const person = new Person(‘John’);

33
Q

What’s object create?

A

const obj = Object.create(proto);

34
Q

What are the different ways to loop through an array in JavaScript?

A

for loop

for … of loop

forEach()

map()

while loop

35
Q

What is a forEach()?

A

Array method that invokes a callback function for each element of the array.

36
Q

what is a map()?

A

Array method that creates a new array by applying a function to each element of the original array.

37
Q

what is a while loop?

A

A loop that continues until a specified condition is no longer true.

38
Q

Explain the difference between “==” and “===” operators in JavaScript?

A

The == operator performs type coercion, meaning it converts the operands to a common type before making the comparison. For example, 1 == ‘1’ would return true because the string is coerced to a number before the comparison.

The === operator, on the other hand, does not perform type coercion. It compares both the value and the type of the operands. For example, 1 === ‘1’ would return false because the operands have different types.

39
Q

What is the purpose of the “setTimeout” function in JavaScript?

A

The setTimeout function is used to schedule the execution of a function or the evaluation of an expression after a specified delay in milliseconds.

It allows you to introduce a time delay in your JavaScript code and execute a particular action or function asynchronously after the delay has elapsed.

40
Q

How do you handle errors in JavaScript?

A

JavaScript provides error handling mechanisms using the try…catch statement.

The try block contains the code that may throw an error, and the catch block allows you to specify the code that will be executed if an error occurs.

Additionally, you can use the finally block to specify code that should be executed regardless of whether an error occurred or not.

Error objects provide information about the type and cause of the error, which can be used for debugging or displaying meaningful error messages to users.

41
Q

Explain the concept of asynchronous programming in JavaScript?

A

Asynchronous programming in JavaScript allows executing code without blocking the execution of subsequent code.

Instead of waiting for an operation to complete before moving to the next line, asynchronous code uses callbacks, promises, or async/await syntax to handle operations that may take time, such as making API calls, reading files, or performing database queries.

Asynchronous programming allows for better responsiveness and avoids blocking the user interface or other processes while waiting for long-running operations to complete.

42
Q

What are callback functions in JavaScript?

A

Callback functions are functions that are passed as arguments to other functions and are called back at a later point in time or after a particular event occurs.

They are commonly used in asynchronous programming to handle the result of an asynchronous operation.

The callback function is executed once the operation completes, allowing you to define the logic that should be executed with the result.

43
Q

What is an AJAX request in JavaScript?

A

AJAX (Asynchronous JavaScript and XML) allows you to make HTTP requests to a server and retrieve data asynchronously without refreshing the entire web page.

In JavaScript, you can use the XMLHttpRequest object or the newer fetch API to make AJAX requests.

44
Q

Explain the concept of promises in JavaScript?

A

Promises are objects used for asynchronous programming in JavaScript.

They represent the eventual completion (or failure) of an asynchronous operation and allow you to handle the result using callbacks.

Promises can be chained together using .then() and .catch() to create a sequence of asynchronous operations

45
Q

What are the three states of a Promise?

A

pending (initial state),

fulfilled (operation completed successfully),

and rejected (operation failed).

46
Q

How do you handle the fulfillment of a promise?

A

then()

47
Q

How do you handle a promise error?

A

catch()

48
Q

What is the purpose of the “let” and “const” keywords in JavaScript?

A

The let and const keywords were introduced in ES6 and are used for declaring block-scoped variables.

49
Q

How do you clone an object in JavaScript?

A

Shallow cloning

Deep cloning

50
Q

Shallow Cloning

A

Use Object.assign({}, obj) or the spread operator {…obj} to create a new object with the same properties and values as the original object.

However, if the object contains nested objects or arrays, they will be shallow copies, meaning changes to the nested objects will affect both the original and cloned objects.

51
Q

Deep Cloning

A

To create a completely independent copy of an object, including nested objects or arrays.

You can use libraries like Lodash’s _.cloneDeep(obj) or write custom recursive cloning functions.

52
Q

Event Bubbling

A

the default behavior where an event triggered on an element is first handled by the innermost element and then propagated up through its ancestors in the DOM tree.

This means that the event handlers of the parent elements will also be triggered if they listen for the same event.

53
Q

Event Capturing

A

the reverse of event bubbling.

In this phase, the event is first captured by the outermost ancestor element and then propagated down to the innermost element.

54
Q

What are the different types of pop-up boxes available in JavaScript?

A

alert()

comfirm()

prompt()

55
Q

alert()

A

Displays a simple message box with an OK button.

56
Q

confirm()

A

Displays a confirmation box with OK and Cancel buttons.

It returns true if the user clicks OK and false if the user clicks Cancel.

57
Q

prompt()

A

Displays an input box where the user can enter text. It returns the text entered by the user or null if the user clicks Cancel.

58
Q

How do you detect the user’s browser in JavaScript?

A

In JavaScript, you can detect the user’s browser using the navigator object, which provides information about the user’s browser and operating system.

For example:

const browserName = navigator.userAgent.toLowerCase();
if (browserName.includes(‘chrome’)) {
console.log(‘Chrome’);
} else if (browserName.includes(‘firefox’)) {
console.log(‘Firefox’);
} else if (browserName.includes(‘safari’)) {
console.log(‘Safari’);
} else if (browserName.includes(‘edge’)) {
console.log(‘Edge’);
} else if (browserName.includes(‘opera’)) {
console.log(‘Opera’);
} else {
console.log(‘Unknown browser’);
}