Intro Flashcards
Why did you change your career path from Biomedical Science to Computer Science?
As much as I enjoyed studying biomedical science, I always felt a tiny part of me was missing that true passion that you enjoy what you are working on. So, I had some time to reflect on myself and I realised that my true passion lies in technology. In every aspect of my life since technology appeared, I always had this fascination about it and desire to know more. I found that this field is a great fit for my skills and interests.
What is JavaScript, and what are its key features?
JavaScript is a programming language used to create interactive and dynamic elements on websites. Its key features include client-side scripting, cross-platform compatibility, event-driven programming, DOM manipulation, asynchronous programming, and support for third-party libraries and frameworks.
How does JavaScript differ from other programming languages?
JavaScript differs from other programming languages in several ways:
Interpretation and Execution: JavaScript is typically executed by web browsers rather than being compiled. This allows for faster development cycles as code changes can be immediately tested without the need for compilation.
Client-Side Execution: JavaScript is primarily used for client-side scripting, running on the user’s web browser. This enables dynamic interaction and manipulation of web page elements without requiring server-side processing.
Focus on Web Development: While JavaScript can be used for other purposes like server-side scripting (Node.js), it is primarily associated with web development. It offers specific features and APIs for working with the Document Object Model (DOM), handling events, and making AJAX calls.
Weak Typing and Dynamic Typing: JavaScript is loosely typed, meaning variables can hold values of different types and can change type during runtime. This flexibility can simplify development but also requires careful handling to avoid unexpected behavior.
Prototype-based Object-Oriented Programming: JavaScript uses a prototype-based approach to object-oriented programming rather than class-based inheritance. Objects can be created directly from existing objects, allowing for flexible object creation and behavior modification.
Functional Programming Support: JavaScript supports functional programming paradigms, allowing functions to be treated as first-class objects, supporting higher-order functions, closures, and immutable data structures.
Widespread Browser Support: JavaScript is supported by all major web browsers, making it a widely accessible language for web development.
These differences make JavaScript unique and suitable for developing interactive web applications.
What are the different data types in JavaScript?
JavaScript has several built-in data types, including:
Number: Represents numeric values, including integers and floating-point numbers.
String: Represents a sequence of characters enclosed in single quotes (‘’) or double quotes (“”).
Boolean: Represents either true or false, used for logical operations and conditional statements.
Null: Represents the intentional absence of any object value.
Undefined: Represents a variable that has been declared but not assigned a value.
Object: Represents a collection of key-value pairs and can include functions, arrays, and other objects.
Array: Represents an ordered list of values, accessible by numeric indices.
Function: Represents a reusable block of code that performs a specific task.
Symbol: Represents a unique identifier, often used as keys for object properties.
These data types provide the foundation for storing and manipulating different kinds of data in JavaScript.
Explain the difference between null and undefined in JavaScript.
null represents the intentional absence of any object value and is assigned explicitly by the programmer. undefined represents the absence of an assigned value and is automatically assigned by JavaScript when a variable is declared but not initialized.
What is the purpose of the “use strict” directive in JavaScript?
The “use strict” directive enables strict mode in JavaScript, enforcing stricter rules and catching common mistakes to improve code quality and reliability.
How do you declare variables in JavaScript? What are the differences between var, let, and const?
Variables in JavaScript can be declared using var, let, or const. var has function or global scope and allows redeclaration and reassignment. let has block scope and allows reassignment but not redeclaration. const also has block scope but is used for variables that remain constant and cannot be reassigned.
Explain the concept of hoisting in JavaScript.
Hoisting is a JavaScript behavior where variable and function declarations are moved to the top of their scope during the compilation phase. Variables declared with var are hoisted, meaning they can be accessed before they are declared, but their assignments remain in place. Function declarations are also hoisted, allowing them to be called before they are defined. However, variables declared with let or const are not hoisted.
What are the different ways to create objects in JavaScript?
There are different ways to create objects in JavaScript:
Object Literal: Objects can be created using curly braces and defining key-value pairs.
Constructor Function: Objects can be created using regular functions with the new keyword.
Object.create(): The Object.create() method creates a new object with a specified prototype.
Class: Introduced in ES6, classes provide a syntax for creating objects with constructor functions and additional features.
How do you handle asynchronous operations in JavaScript? Provide examples.
Asynchronous operations in JavaScript can be handled using callbacks, Promises, or async/await.
Callbacks:
fetchData(function (data) {
console.log(data);
});
Promises:
fetchData()
.then(function (data) {
console.log(data);
});
Async/Await:
async function processData() {
const data = await fetchData();
console.log(data);
}
processData();
Callbacks provide a traditional approach, Promises offer better control flow, and async/await provides a more synchronous-like coding style for handling asynchronous operations.
Explain the concept of closures in JavaScript and provide an example
Closures in JavaScript allow functions to retain access to variables from their outer scope even after the outer function has finished executing.
function outerFunction() {
const outerVariable = “Hello”;
function innerFunction() {
console.log(outerVariable);
}
return innerFunction;
}
const myFunction = outerFunction();
myFunction(); // Outputs: “Hello”
The inner function innerFunction has access to the outerVariable from its enclosing scope, creating a closure. The closure allows innerFunction to access and use the outerVariable even after outerFunction has completed.
How does prototypal inheritance work in JavaScript?
Prototypal inheritance in JavaScript allows objects to inherit properties and methods from other objects. Each object has a [[Prototype]] property that points to another object. When a property is accessed on an object, JavaScript checks the object itself first, and if not found, it looks up the prototype chain until it finds the property or reaches the end.
What is the Event Loop in JavaScript, and how does it handle asynchronous operations?
The Event Loop is a fundamental mechanism in JavaScript that handles asynchronous operations. It ensures that code execution continues while asynchronous tasks are being processed in the background.
Here’s a simplified explanation:
JavaScript code is executed line by line.
Asynchronous operations are scheduled to run in the future and are moved out of the main thread.
The rest of the code continues executing.
Once an asynchronous operation completes, it is placed in the task queue.
The Event Loop constantly checks if the call stack is empty.
If the call stack is empty, tasks from the task queue are pushed onto the call stack for execution.
This process allows JavaScript to handle asynchronous tasks without blocking the main thread and keeps the code responsive.
Describe the difference between synchronous and asynchronous code execution in JavaScript.
Synchronous code execution runs line by line, blocking subsequent code execution until each line is completed. Asynchronous code execution allows operations to run in the background while the program continues to execute other code without waiting for their completion. Asynchronous operations are typically scheduled for future execution and utilize callbacks, Promises, or async/await to handle their results.
How do you handle errors in JavaScript? What are the error handling mechanisms available?
In JavaScript, errors can be handled using try…catch statements, throwing errors using the throw statement, using built-in error objects, and error event handlers. Try…catch statements catch exceptions within a block of code, while the throw statement manually throws an error. Built-in error objects provide additional information about errors, while error event handlers handle errors that occur during asynchronous tasks. Promises and async/await can also handle errors using the catch block or try…catch statements.