Full Stack Software Engineer
1025 flashcards · Programming Languages · by danielemiller
What is the spread operator in JavaScript?
The spread operator is represented by three dots (…) and is used to expand elements in an array or object.
How can the spread operator be used with arrays?
The spread operator can be used to copy an array or merge two or more arrays. For example: const arr1 = [1, 2, 3]; const arr2 = [4, 5, 6]; const arr3 = [...arr1, ...arr2]; console.log(arr3); // [1, 2, 3, 4, 5, 6]
How can the spread operator be used with objects?
The spread operator can be used to copy or merge properties in objects. For example: const obj1 = {a: 1, b: 2}; const obj2 = {c: 3, d: 4}; const obj3 = {...obj1, ...obj2}; console.log(obj3); // {a: 1, b: 2, c: 3, d: 4}
How can the spread operator be used with function arguments?
The spread operator can be used to pass an array or object as arguments to a function. For example: (passing an array) function myFunction(a, b, c) { console.log(a, b, c); } const arr = [1, 2, 3]; myFunction(...arr); // 1 2 3 (passing an object) function printPersonInfo(person) { console.log(`Name: ${person.name}`); console.log(`Age: ${person.age}`); console.log(`Gender: ${person.gender}`); } const person = { name: 'Jane', age: 30, gender: 'female', location: 'New York' }; console.log(Object.ke…
When can the Effect Hook be utilized?
The Effect Hook can be utilized in three key moments: When the component is first added, or mounted, to the DOM and renders When the state or props change, causing the component to re-render When the component is removed, or unmounted, from the DOM
When does the Effect Hook trigger a function?
The Effect Hook triggers a function when any of the three key moments occur - when the component mounts, re-renders, or unmounts.
Why is the Effect Hook useful?
The Effect Hook is useful for handling side effects, such as fetching data or setting up event listeners, that depend on the component's lifecycle or state. By utilizing the Effect Hook, we can ensure that these side effects are executed at the appropriate times and cleaned up when necessary.
Why is the order in which hooks are called in a React function component important?
The order in which hooks are called in a React function component is important because it can affect the behavior and performance of the component.
When should the State Hook be called in a React function component?
The State Hook (useState()) should be called at the beginning of the component function, before any other hooks or event handlers.
Why should the State Hook be called before any other hooks or event handlers?
The State Hook is responsible for managing the component's state, and any updates to the state can trigger re-renders of the component. By calling the State Hook first, we ensure that the state is set up correctly before any other hooks or event handlers are executed.
When should the Effect Hook be called in a React function component?
The Effect Hook (useEffect()) should be called after the State Hook and any other hooks that may be needed.
Why should the Effect Hook be called after the State Hook?
The Effect Hook is used to manage side effects that depend on the component's state or props. By calling the Effect Hook after the State Hook, we ensure that the effect has access to the current state values and can update them as needed.
When should event handlers be defined in a React function component?
Event handlers should be defined inside the component function, and called after the State Hook and any other hooks have been called. This ensures that the event handler has access to the current state and any other hooks that may be needed.
What are template literals in JavaScript?
Template literals are literals delimited with backtick (`) characters in JavaScript. They allow for multi-line strings, string interpolation with embedded expressions, and special constructs called tagged templates. Note: String interpolation with embedded expressions means that we can embed expressions, variables, or function calls directly within a string by wrapping them in ${}. This allows us to dynamically generate strings based on the values of variables or the results of functions.
Why is it important to clean up effects in React?
Some effects in React require cleanup to avoid memory leaks. For example, when we add event listeners to the DOM outside of our component, it's important to remove those event listeners when we're done with them. Otherwise, the event listeners can cause memory leaks, which can slow down the application and potentially cause crashes. By cleaning up effects, we ensure that the application remains fast and responsive, and that it uses memory efficiently.
How can we run an effect only when a component mounts in React?
We can run an effect only when a component mounts in React by using the Effect Hook and passing an empty array as the second argument to the useEffect() method. This tells React to only call the effect after the first render, and to skip it on subsequent renders.
What is the dependency array in React?
The dependency array is a second argument that can be passed to the useEffect() method in React. It is used to tell the useEffect() method when to call our effect and when to skip it. Our effect is always called after the first render, but only called again if something in our dependency array has changed values between renders. This can improve the performance of our application and prevent unnecessary updates to the DOM.
What are the two main rules to keep in mind when using Hooks?
The two main rules to keep in mind when using Hooks are: Only call Hooks at the top level. Only call Hooks from React functions.
What are the different cases for when an Effect is called based on the Dependency Array in React?
The different cases for when an Effect is called based on the Dependency Array in React are: undefined: Effect is called after the first render and every re-render. Empty array: Effect does not re-render. Non-empty array: Effect is called when any value in the dependency array changes.
When is the cleanup function in a React effect called?
The cleanup function in a React effect is called: When the component unmounts, which means that it's called when the component is removed from the DOM. When the effect is re-run, which can happen when the dependencies of the effect change, or when the component re-renders due to other reasons such as state updates or prop changes. By including a cleanup function in the effect, you can ensure that any resources used by the effect, such as event listeners, intervals, or subscriptions, are properl…
What does the filter() method in JavaScript do?
The filter() method in JavaScript is used to create a new array with all elements that pass a certain test. The filter() method does not change the original array; instead, it returns a new array that contains only the elements that pass the test specified in the callback function. Here's an example of how to use the filter() method to filter out even numbers from an array: const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const oddNumbers = numbers.filter((num) => { return num % 2 !== 0; }); consol…
What is the relationship between files in a Node.js project?
Every file in a Node.js project is invisible to every other file, except for what you choose to expose via module.exports. This means that you could have 100 different files, all with global variables named style, and there would be no conflicts as long as you only expose the necessary variables via module.exports. This allows for greater modularity and flexibility in organizing and managing code within a Node.js project.
What is code coverage?
Code coverage is a measure of how much of a software application's source code is executed during testing. It is a metric that indicates how thoroughly the application has been tested.
How is code coverage usually expressed?
Code coverage is usually expressed as a percentage of lines of code or functions that have been executed by the tests.
Does high code coverage guarantee that an application is free of bugs or that it meets all requirements?
No, high code coverage does not necessarily mean that the application is free of bugs or that it meets all requirements, but it does give an indication of how well the application has been tested.
Is code coverage available for testing frameworks other than Jest?
Yes, code coverage is available for other testing frameworks besides Jest, such as Mocha, Ava, and Jasmine. These testing frameworks can be used with code coverage libraries like Istanbul and NYC to generate coverage reports.
What are some examples of code coverage libraries?
Some examples of code coverage libraries include Istanbul and NYC for JavaScript, Jacoco for Java, Coverlet for .NET, SimpleCov for Ruby, and gcov for C and C++. These tools can help you measure how thoroughly your application has been tested, and identify areas of the code that need additional testing.
What is Jest?
Jest is a unit testing framework that makes testing React applications easy. It works seamlessly with React and includes an entire library of predefined tests with the ability to mock functions.
What is Enzyme?
Enzyme is designed to test React components and it’s a great way to write assertions (or scenarios) that simulate actions that confirm the front-end UI is working correctly. It seeks out components on the front end, interacts with them, and raises a flag if any of the components aren’t working the way it’s told they should.
What is snapshot testing?
Snapshot testing is used to keep track of changes in the app UI. It captures the code of a component at a moment in time so we can compare the component in one state versus any other possible states it might take.
What is a mock function in Enzyme?
A mock function in Enzyme is a function that simulates props being passed between components. It can be used to confirm whether props from one component are being passed to another.
How can you mock API calls with Jest?
You can mock API calls with Jest by creating a new folder called mock in the same directory where the tests folder lives. Then, you can create a mock request file that replaces the actual API call with a simulated response.
What are some best practices for testing in React applications?
Some best practices for testing in React applications include: creating a setup file for tests, using snapshot testing to keep track of changes in the app UI, testing component props to ensure they are being passed correctly, and mocking API calls to avoid actually hitting the API.
What is the spyOn() module in Jest?
The spyOn() module is a feature of the Jest testing framework that allows developers to create mock functions that can be used to verify whether other functions or methods are called during a test.
Why would we use the spyOn() module in Jest?
We would use the spyOn() module in Jest to verify that specific functions or methods are called during a test, and to ensure that code is executing as expected. This can help with debugging and prevent issues caused by functions that are not called correctly or not called at all. In the case of API calls, using spyOn() can also be helpful because it allows us to test the functionality of our code without making actual calls to an external API. This is important because APIs may have call limits…
How do you use the spyOn() module in Jest?
To use the spyOn() module in Jest, you can call the jest.spyOn() method and pass in the object containing the method you want to spy on, along with the name of the method. You can then use the toBeCalled() or toHaveBeenCalled() methods to verify whether the method was called during the test.
Can the spyOn() module be used with any function or method?
The spyOn() module can be used with most functions or methods, but it may not work with functions or methods that are defined inside a closure or that are not defined on a global object. In some cases, it may be necessary to refactor the code in order to use spyOn() effectively.
What are some common types of servers?
Some common types of servers are: file server database server web server mail server application server proxy server
What is a file server used for?
A file server is used to store data files for multiple users. They provide users access to stored files and data and can allow faster retrieval and saving of data. These are used on private networks and provide a location for storage of computer files. It provides a central location to store files where multiple users can work with the same documents.
What is a database server used for?
A database server allows another computer to access the database and retrieve or upload data. This type of server typically has a large storage capacity.
What is a web server used for?
A web server delivers web pages requested by multiple client web browsers.
What are mail servers used for?
Mail servers store and deliver email for clients through an email service platform. They are a form of digital ‘post office’ that sorts and stores emails.
What is an application server used for?
An application server provides a software environment with all the needed requirements. This server allows users to access the application without needing to download additional software.
What is a proxy server used for?
The proxy server communicates with the websites you are visiting on your behalf. It links the user with the rest of the internet. The browser connects to the proxy server, and the proxy server sends your request to the website and sends the website’s response back to you.
What is a process in computer science?
A process in computer science is the instance of a computer program that is being executed.
How can you view the processes running on your computer?
You can open Task Manager if you’re on a Windows machine or Activity Monitor from a Mac to see information about the various processes running on your computer right now.
What is an example of information stored in the process.env object?
An example of information stored in the process.env object is the PWD property which holds a string with the directory in which the current process is located.
Why is it useful to have if/else logic based on the current environment?
It is useful to have if/else logic in a program depending on the current environment— a web application in a development phase might perform different tasks than when it’s live to users. We could store this information on the process.env.
What is the convention for adding a property to process.env?
The convention for adding a property to process.env is to use the key NODE_ENV and a value of either production or development.
What can the term "heap" refer to in different contexts?
The term "heap" can refer to a specific data structure or a block of computer memory, depending on the context.
What is stored in the process.argv property?
The process.argv property holds an array of command line values provided when the current process was initiated.
What does the process.memoryUsage().heapUsed method return?
The process.memoryUsage().heapUsed method returns a number representing how many bytes of memory the current process is using.
What are 6 common OS modules?
os.type() — to return the computer’s operating system. os.arch() — to return the operating system CPU architecture. os.networkInterfaces() — to return information about the network interfaces of the computer, such as IP and MAC address. os.homedir() — to return the current user’s home directory. os.hostname() — to return the hostname of the operating system. os.uptime() — to return the system uptime, in seconds.
What is util.promisify() in Node.js?
util.promisify() is a utility function in Node.js that converts a callback-based function into a Promise-based function.
What does util.promisify() do?
util.promisify() creates a new function that returns a Promise instead of accepting a callback as an argument.
How does util.promisify() work?
To use util.promisify(), pass the callback-based function as an argument to util.promisify(). The resulting function can then be used with .then() and .catch() or in a Promise chain.
What is the purpose of util.promisify()?
The purpose of util.promisify() is to simplify working with callback-based APIs by allowing you to use Promises instead. It's especially useful when you need to work with APIs that use callbacks but prefer to use Promises for asynchronous code.
What is Node's architecture described as?
Node's architecture is often described as event-driven.
What is the difference between traditional imperative programming and event-driven programming?
In traditional imperative programming, we give the computer a series of instructions to execute in a pre-defined order. In event-driven programming, we write logic to handle situations without knowing exactly when they'll occur.
Can you give an example of a situation in web development where event-driven programming is necessary?
When programming a website, we might provide functionality for a click event without knowing when a user will trigger it.
What programming concept did Node apply to the back-end environment?
Node applied the same concept of event-driven principles to the back-end environment.
Why is event-driven programming important in web development?
Event-driven programming allows us to handle user interactions and other situations that occur unpredictably in web development.
How does event-driven programming differ from traditional programming?
Event-driven programming is focused on handling events or situations as they occur, rather than following a pre-defined series of instructions.
What is the EventEmitter class in Node.js?
The EventEmitter class is a built-in class in Node.js that allows developers to easily implement event-driven programming in their applications.
What are the two primary methods provided by the EventEmitter class?
The two primary methods provided by the EventEmitter class are emit(event, args) and on(event, listener).
What is the purpose of the emit() method?
The emit() method is used to trigger an event with the specified name and any additional arguments that need to be passed along with it.
What is the purpose of the on() method?
The on() method is used to register a listener function that will be executed whenever the specified event is triggered.
How can the EventEmitter class be extended in a Node.js application?
The EventEmitter class can be extended by any other class that needs to emit events and handle listeners for those events.
What is the benefit of using the EventEmitter class in a Node.js application?
The EventEmitter class allows developers to easily handle user interactions, server requests, and other asynchronous events in a simple and effective way.
What is process.stdout in Node.js?
process.stdout is a writable stream object in Node.js that allows you to write output to the console in real-time.
What is process.stdin in Node.js?
process.stdin is a readable stream object in Node.js that allows you to read input from the console in real-time.
How is process.stdout different from console.log?
process.stdout is a stream that allows you to write output to the console in real-time, while console.log logs information to the console and adds a newline character at the end of the message.
When should you use console.log in Node.js?
console.log is designed to be used for logging information, such as debugging your application.
When should you use process.stdout in Node.js?
You should use process.stdout when you need to output data to the console in real-time.
What is an error-first callback in JavaScript?
An error-first callback in JavaScript is a function that takes an error object as its first argument and a result object as its second argument. The error object is null if no error occurred, and contains information about the error if one occurred. The result object contains the result of the function if it completed successfully.
Why are error-first callbacks used in JavaScript?
Error-first callbacks are used in JavaScript to handle errors that might occur during the execution of asynchronous code, such as I/O operations, network requests, or database queries. By passing an error-first callback to an asynchronous function, we can handle any errors that might occur and respond appropriately.
What is the convention for naming an error-first callback in JavaScript?
The convention for naming an error-first callback in JavaScript is to use the word callback as the last argument name, preceded by any other arguments that the function might take. For example, if a function takes two arguments and an error-first callback, the function signature might look like this: function myFunction(arg1, arg2, callback).
How do we invoke an error-first callback in JavaScript?
To invoke an error-first callback in JavaScript, we pass null as the first argument if the function completed successfully, or an error object if an error occurred. We then pass the result of the function as the second argument. For example, to invoke an error-first callback named callbackFunc with an error object and no result, we would write: callbackFunc(new Error('Something went wrong.'), null).
What is the role of the error-first callback in the Node.js event loop?
In the Node.js event loop, error-first callbacks are used to handle asynchronous I/O operations that are triggered by events. When an event occurs, the associated callback is added to the event loop, and when the callback is executed, it is passed an error object and a result object (if any). The callback then handles the error and/or result as appropriate.
What is the Buffer module in Node.js used for?
The Buffer module in Node.js is used to handle binary data. It provides a way to represent and manipulate binary data in a more efficient and flexible way than simply treating it as a string of characters.
How are Buffer objects represented in Node.js?
Buffer objects in Node.js are represented as a fixed amount of memory that cannot be resized. They are similar to an array of integers, where each element in the array represents a byte of data.
What are some methods provided by the Buffer module in Node.js?
he Buffer module in Node.js provides several methods for working with binary data, including .alloc(), .toString(), .from(), and .concat().
What does the .alloc() method do in the Buffer module?
The .alloc() method creates a new Buffer object with a specified size and optional fill value. It returns a new Buffer object filled with zeroes or the specified fill value. const buffer = Buffer.alloc(5); console.log(buffer); // Ouput: [0, 0, 0, 0, 0]
What does the .toString() method do in the Buffer module?
The .toString() method translates the contents of a Buffer object into a human-readable string. It accepts optional arguments for specifying the character encoding, start byte offset, and end byte offset. const buffer = Buffer.alloc(5, 'a'); console.log(buffer.toString()); // Output: aaaaa
What does the .from() method do in the Buffer module?
The .from() method creates a new Buffer object from a specified string, array, or buffer. It returns a new Buffer object filled with the contents of the specified object. const buffer = Buffer.from('hello'); console.log(buffer); // Output: [104, 101, 108, 108, 111]
What does the .concat() method do in the Buffer module?
The .concat() method joins multiple Buffer objects into a single Buffer object. It returns a new Buffer object containing the concatenated contents of the specified Buffer objects. const buffer1 = Buffer.from('hello'); // Output: [104, 101, 108, 108, 111] const buffer2 = Buffer.from('world'); // Output:[119, 111, 114, 108, 100] const array = [buffer1, buffer2]; const bufferConcat = Buffer.concat(array); console.log(bufferConcat); // Output: [104, 101, 108, 108, 111, 119, 111, 114, 108, 100]
What is fs.readFile() in Node.js?
fs.readFile() is a method in the Node.js file system (fs) module that is used to read the contents of a file asynchronously.
The syntax for fs.readFile() in Node.js is:
fs.readFile(path[, options], callback) Here, path is the path to the file that should be read, options is an optional object specifying options for how the file should be read (such as the character encoding), and callback is a callback function that will be called with two arguments when the file is read: an error (if one occurred) and the contents of the file.
How do you use fs.readFile() in Node.js?
To use fs.readFile() in Node.js, you first need to require the fs module: const fs = require('fs'); You can then call fs.readFile() with the path to the file you want to read and a callback function that will be called when the file is read. For example, to read the contents of a file named example.txt and log them to the console: fs.readFile('example.txt', (err, data) => { if (err) throw err; console.log(data); }); If an error occurs while reading the file, the callback function will be called…
What is a stream in Node.js?
A stream in Node.js is a way of processing data sequentially, piece by piece, rather than all at once. Streaming data is often preferable when working with large amounts of data, since it allows you to process data in smaller, more manageable chunks.
How can you read a file line-by-line in Node.js?
To read a file line-by-line in Node.js, you can use the .createInterface() method from the readline core module. This method creates an EventEmitter that emits 'line' events for each line of the file. Here's an example: const readline = require('readline'); const fs = require('fs'); const myInterface = readline.createInterface({ input: fs.createReadStream('text.txt') }); myInterface.on('line', (fileLine) => { console.log(`The line read: ${fileLine}`); });
What is the readline core module in Node.js?
The readline core module in Node.js provides an interface for reading input (such as user input or file input) line-by-line. It is particularly useful for processing text data that is too large to fit in memory all at once.
What does the .createInterface() method do in the readline module?
The .createInterface() method in the readline module creates a new Interface object, which is an EventEmitter that emits 'line' events for each line of input. You can specify the input source (such as a file stream or a readable stream) as an option when creating the Interface object.
What does the .on() method do in the Interface object?
The .on() method in the Interface object is used to register an event listener for a specific event (such as 'line'). When the event is emitted, the listener function will be called with the appropriate arguments (such as the line of text).
How can you write data to a stream in Node.js?
To write data to a stream in Node.js, you can create a writable stream using the fs.createWriteStream() method from the fs module. This method creates a new writable stream to a file and returns it as a stream object. You can then write data to the stream using the .write() method and indicate the end of the stream with the .end() method.
What is a writable stream in Node.js?
A writable stream in Node.js is a type of stream that you can write data to. It represents a destination for data, such as a file, socket, or HTTP response.
What is the syntax for creating a writable stream in Node.js?
The syntax for creating a writable stream in Node.js is: const stream = fs.createWriteStream(path[, options]) Here, path is the path to the file you want to write to, and options is an optional object specifying options for how the file should be written (such as the character encoding).
What is the difference between a readable and writable stream in Node.js?
A readable stream in Node.js is a type of stream that you can read data from, while a writable stream is a type of stream that you can write data to. Readable streams emit data events when there is data available to be read, while writable streams have a .write() method that you can use to write data to the stream.
How can you indicate the end of a writable stream in Node.js?
To indicate the end of a writable stream in Node.js, you can call the .end() method on the stream object. This method signals that no more data will be written to the stream, and any remaining data in the write buffer will be flushed.
What is the purpose of a server in Node.js?
The purpose of a server in Node.js is to listen for requests, perform whatever action is required to satisfy the request, and then return a response. A server allows clients to communicate with your application over the internet, and is an essential component of building web applications.