Function declaration
For example:
function myFunction() {
// function body
}Function expression
For example:
const myFunction = function() {
// function body
};Concept of scope and how it works in JavaScript
Different ways to create objects in JavaScript
Object literal code example
For example:
const myObject = {
key1: value1,
key2: value2,
// ...
};Constructor function code example
For example:
function MyObject(key1, key2) {
this.key1 = key1;
this.key2 = key2;
}
const myObject = new MyObject(value1, value2);Object.create() code example
For example:
const proto = {
key1: 'no value',
2: 'no value'
}
const myObject = Object.create(proto);
myObject.key1 = value1;
myObject.key2 = value2;ES6+ Class Syntax code example
For example:
class MyObject {
constructor(key1, key2) {
this.key1 = key1;
this.key2 = key2;
}
}
const myObject = new MyObject(value1, value2);“this” keyword behavior in regular functions
“this” keyword behavior in arrow functions
Arrow functions do not bind their own this value.
Therefore if this is used in an arrow function, it will not refer to the parent scope, rather it will refer to the lexical or ancestral scope of the arrow function.
Methods available for iterating over an object’s properties
for … in code example
Example:
const aPerson = {
name: "Brad Jones",
age: 44,
gender: "male",
ethnicity: "white",
citizenship: "USA",
};
for (const key in aPerson) {
if (Object.hasOwnProperty.call(aPerson, key)) {
const element = aPerson[key];
console.log(element);
}
}Object.keys() code example
const keys = Object.keys(object);
keys.forEach((key) => {
const value = object[key];
// Use key and value
});
Object.values() code example
const values = Object.values(object);
values.forEach((value) => {
// Use value
});
Object.entries() code example
For example:
const entries = Object.entries(object);
entries.forEach(([key, value]) => {
// Use key and value
});How does the envent loop enable asynchronous behavior?
How do you handle asynchronous errors using Promises?
Promises have a .catch() method to handle errors that occur during asynchronous operations.
For Example:
asyncFunction()
.then((result) => {
// Handle success
})
.catch((error) => {
// Handle error
});
How do you handle asynchronous errors using async/await?
Async functions can use try/catch blocks to handle errors.
For Example:
async function myFunction() {
try {
const result = await asyncFunction();
// Handle success
} catch (error) {
// Handle error
}
}
How do you handle asynchronous errors using Callbacks?
Callback functions can check for errors as the first argument passed to the callback.
For Example:
asyncFunction((error, result) => {
if (error) {
// Handle error
} else {
// Handle success
}
});
null
null is an assignment value that represents the intentional absence of any object value.
It is a primitive value that signifies the absence of a value.
undefined
Promise
async/await
How does async/await simplify working with asynchronous code?