DEV Community

Atchukola Naresh
Atchukola Naresh

Posted on

JavaScript Technical Paper

For loop:

A for loop in JavaScript is a control flow statement that allows you to execute a block of code repeatedly until a specified condition is met.
Code snippet

for (initialization; condition; iteration) {
  // block of code to be executed repeatedly
}
Enter fullscreen mode Exit fullscreen mode

A for loop in JavaScript is a control flow statement that allows you to execute a block of code repeatedly until a specified condition is met. The syntax for a for loop is as follows:

Code snippet

for (initialization; condition; iteration) {
  // block of code to be executed repeatedly
}

Enter fullscreen mode Exit fullscreen mode

The initialization expression is executed once, before the first iteration of the loop. The condition expression is evaluated before each iteration of the loop. If the condition evaluates to true, the block of code is executed. If the condition evaluates to false, the loop terminates. The iteration expression is executed after each iteration of the loop.

Here is an example of a for loop that prints the numbers from 1 to 10:

Code snippet

for (var i = 1; i <= 10; i++) {
  console.log(i);
}
Enter fullscreen mode Exit fullscreen mode

This loop will first initialize the variable i to 1. Then, it will evaluate the condition i <= 10. Since i is equal to 1, the condition evaluates to true. The block of code is then executed, which prints the value of i to the console. The loop then increments i by 1 and repeats the process. This will continue until i is greater than 10, at which point the condition will evaluate to false and the loop will terminate.

forEach :

The forEach() method in JavaScript is used to iterate over an array and execute a function for each element in the array.

The forEach() method is a convenient way to iterate over an array and execute a function for each element. It is often used to perform simple tasks, such as logging the elements of an array to the console or updating the values of an array.

Code snippet

// Log the elements of an array to the console.
const arr = [1, 2, 3, 4, 5];

arr.forEach(function(element) {
  console.log(element);
});

// Update the values of an array.
const arr = [1, 2, 3, 4, 5];

arr.forEach(function(element) {
  element *= 2;
});

console.log(arr); // [2, 4, 6, 8, 10]
Enter fullscreen mode Exit fullscreen mode

for (...in..):

The for ... in loop in JavaScript is used to iterate over the properties of an object. It can be used to print the properties of an object, or to access the values of the properties. The syntax for the for ... in loop is as follows:
Code snippet

for (let key in object) {
  // Do something with the key and the value of object[key]
}
Enter fullscreen mode Exit fullscreen mode

Code snippet

const object = {
  name: "John Doe",
  age: 30,
  address: "123 Main Street"
};

for (let key in object) {
  console.log(key, object[key]);
}
Enter fullscreen mode Exit fullscreen mode

This code will print the following output:

Code snippet

name John Doe
age 30
address 123 Main Street
Enter fullscreen mode Exit fullscreen mode

for (..of..):

The for ... of loop in JavaScript is used to iterate over the values of an iterable object. It can be used to print the values of an iterable object, or to access the values of the iterable object. The syntax for the for ... of loop is as follows:

Code snippet

for (let value of iterable) {
  // Do something with the value
}
Enter fullscreen mode Exit fullscreen mode

Code snippet

const iterable = [1, 2, 3, 4, 5];

for (let value of iterable) {
  console.log(value);
}
Enter fullscreen mode Exit fullscreen mode

This code will print the following output:

Code snippet

1
2
3
4
5
Enter fullscreen mode Exit fullscreen mode

The for ... of loop can also be used to iterate over the values of a string. For example, the following code will print the characters in the string:

Code snippet

const string = "Hello, world!";

for (let value of string) {
  console.log(value);
}
Enter fullscreen mode Exit fullscreen mode

This code will print the following output:

Code snippet

H
e
l
l
o
,

w
o
r
l
d
!
Enter fullscreen mode Exit fullscreen mode

while loop:

A while loop in JavaScript is a programming construct that allows you to execute a block of code repeatedly as long as a given condition is true.

Code snippet

while (condition) {
  // block of code to be executed repeatedly
}

Enter fullscreen mode Exit fullscreen mode

Here is an example of a while loop that prints the numbers from 1 to 10:

Code snippet

var i = 1;
while (i <= 10) {
  console.log(i);
  i++;
}
Enter fullscreen mode Exit fullscreen mode

Mutable and Immutable Methods (in strings);

In JavaScript, strings are immutable, which means that once a string is created, its value cannot be changed. This is in contrast to mutable objects, such as arrays, which can be changed after they are created.

There are a number of methods in JavaScript that can be used to manipulate strings. Some of these methods are mutable, while others are immutable.

Mutable methods

  • concat(): This method concatenates the specified string to the end of the current string.
  • replace(): This method replaces all occurrences of the specified substring with the specified replacement string.
  • slice(): This method returns a new string that is a substring of the current string.

Immutable methods

  • toUpperCase(): This method returns a new string that is the uppercase version of the current string.
  • toLowerCase(): This method returns a new string that is the lowercase version of the current string.
  • trim(): This method returns a new string that has all leading and trailing whitespace characters removed.

It is important to be aware of the difference between mutable and immutable methods when working with strings. If you are using a mutable method, you should be careful not to modify the string in a way that could affect other parts of your code.

Mutable and Immutable Methods (in arrays):

In JavaScript, arrays are mutable objects, which means that their contents can be changed after they have been created. This is done by using the various array methods, such as push(), pop(), shift(), unshift(), reverse(), sort(), and splice().

Mutable Array Methods

  • push(): Adds an element to the end of the array.
  • pop(): Removes the last element from the array and returns it.
  • shift(): Removes the first element from the array and returns it.
  • unshift(): Adds an element to the beginning of the array.
  • reverse(): Reverses the order of the elements in the array.
  • sort(): Sorts the elements in the array.
  • splice(): Removes and/or adds elements from the array.

Immutable Array Methods

There are also a number of immutable array methods that do not mutate the original array. These methods return a new array with the changes applied.

  • map(): Applies a function to each element in the array and returns a new array with the results.
  • filter(): Filters the array by returning only the elements that meet a certain condition.
  • reduce(): Reduces the array to a single value by applying a function to each element.

JavaScript

function swap(a, b) {
  const temp = a;
  a = b;
  b = temp;
}

const x = 1;
const y = 2;

swap(x, y);

console.log(x, y); // 2, 1

Enter fullscreen mode Exit fullscreen mode

Use code with caution. Learn more

content_copy

In this example, the swap() function takes two arguments: a and b. The a and b arguments are references to the variables x and y. When the swap() function is called, the values of a and b are swapped. The changes to a and b are reflected in the calling function, because a and b are references to the variables x and y.

- Pass by Reference and Pass by Value - add codeblocks;

Pass by reference

JavaScript

function swap(a, b) {
  const temp = a;
  a = b;
  b = temp;
}

const x = 1;
const y = 2;

swap(x, y);

console.log(x, y); // 2, 1

Enter fullscreen mode Exit fullscreen mode

In this example, the swap() function takes two arguments: a and b. The a and b arguments are references to the variables x and y. When the swap() function is called, the values of a and b are swapped. The changes to a and b are reflected in the calling function, because a and b are references to the variables x and y.
Pass by value

JavaScript

function add(a, b) {
  return a + b;
}

const x = 1;
const y = 2;

const z = add(x, y);

console.log(z); // 3

Enter fullscreen mode Exit fullscreen mode

In this example, the add() function takes two arguments: a and b. The a and b arguments are the values of the variables x and y. When the add() function is called, the values of a and b are added together and the result is returned. The changes to a and b are not reflected in the calling function, because a and b are the values of the variables x and y.

In general, pass by reference is used when you want to modify the value of an argument in the calling function. Pass by value is used when you only want to use the value of an argument in the called function.

Array methods - add codeblocks and mention mutable/immutable;

Array.pop-

The array.pop() method in JavaScript is used to remove the last element from an array and returns that element. This method changes the length of the array so it was a
mutable method.

The array.pop() method takes no arguments.

Here is an example of how to use the array.pop() method:

JavaScript

const numbers = [1, 2, 3, 4, 5];

const lastNumber = numbers.pop();

console.log(lastNumber); // 5

console.log(numbers); // [1, 2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

Array.push:

The array.push() method in JavaScript is used to add one or more elements to the end of an array. This method changes the length of the array.

The array.push() method takes one or more arguments, which are the elements to be added to the end of the array.

Here is an example of how to use the array.push() method:

JavaScript

const numbers = [1, 2, 3];

numbers.push(4, 5);

console.log(numbers); // [1, 2, 3, 4, 5]

Enter fullscreen mode Exit fullscreen mode

- Array.concat:

The array.concat() method in JavaScript is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

The array.concat() method takes one or more arguments, which are the arrays to be merged.

Here is an example of how to use the array.concat() method:

JavaScript

const numbers1 = [1, 2, 3];
const numbers2 = [4, 5, 6];

const numbers3 = numbers1.concat(numbers2);

console.log(numbers3); // [1, 2, 3, 4, 5, 6]
Enter fullscreen mode Exit fullscreen mode

- Array.slice:

The array.slice() method in JavaScript is used to extract a portion of an array into a new array. This method does not change the original array, but instead returns a new array.

The array.slice() method takes three arguments:

  • The start index of the portion to be extracted.
  • The end index of the portion to be extracted.
  • The optional third argument is a boolean value that indicates whether or not to include the end index in the extracted portion. If the third argument is omitted or false, the end index is not included in the extracted portion.

Here is an example of how to use the array.slice() method:

JavaScript

const numbers = [1, 2, 3, 4, 5];

const numbers2 = numbers.slice(1, 3);

console.log(numbers2); // [2, 3]
Enter fullscreen mode Exit fullscreen mode

- Array.splice:

array.splice() method in JavaScript is used to remove, add, or replace elements in an array. This method changes the original array, and returns the removed elements as a new array.

The array.splice() method takes four arguments:

  • The index of the first element to be removed, added, or replaced.
  • The number of elements to be removed.
  • The elements to be added, or an empty array if no elements are to be added.
  • An optional fifth argument that indicates whether or not to return the removed elements. If the fifth argument is omitted or false, the removed elements are not returned.

Here is an example of how to use the array.splice() method to remove elements from an array:

JavaScript

const numbers = [1, 2, 3, 4, 5];

numbers.splice(1, 2);

console.log(numbers); // [1, 4 ,5]

Enter fullscreen mode Exit fullscreen mode

In this example, the array.splice() method is called on the numbers array. The array.splice() method removes the elements at index 1 and 2, inclusive. The numbers array is then logged to the console. The numbers array now only contains the numbers 1 ,4, 5.

Here is an example of how to use the array.splice() method to add elements to an array:

JavaScript

const numbers = [1, 2, 3, 4, 5];

numbers.splice(2, 0, 6, 7);

console.log(numbers); // [1, 2, 6, 7, 3, 4, 5]

Enter fullscreen mode Exit fullscreen mode

In this example, the array.splice() method is called on the numbers array. The array.splice() method adds the numbers 6 and 7 at index 2. The numbers array is then logged to the console. The numbers array now contains the numbers 1, 2, 6, 7, 3, 4, and 5.

Here is an example of how to use the array.splice() method to replace elements in an array:

JavaScript

const numbers = [1, 2, 3, 4, 5];

numbers.splice(2, 1, 6);

console.log(numbers); // [1, 2, 6, 4, 5]

Enter fullscreen mode Exit fullscreen mode

In this example, the array.splice() method is called on the numbers array. The array.splice() method replaces the element at index 2 with the number 6. The numbers array is then logged to the console. The numbers array now contains the numbers 1, 2, 6, 4, and 5.

- Array.join:

The Array.join() method in JavaScript is used to join the elements of an array into a string. The elements of the string will be separated by a specified separator and its default value is a comma(, ).

The syntax for the Array.join() method is:

Code snippet

array.join(separator)
Enter fullscreen mode Exit fullscreen mode
  • array is the array to be joined
  • separator is the string that will be used to separate the elements of the array. If no separator is specified, a comma will be used by default.

For example, the following code will join the elements of the array variable into a string, separated by commas:

Code snippet

const array = [1, 2, 3, 4];
const joinedArray = array.join(',');

console.log(joinedArray); // "1,2,3,4"

Enter fullscreen mode Exit fullscreen mode

The Array.join() method can also be used to join the elements of an array into a string, separated by a custom separator. For example, the following code will join the elements of the array variable into a string, separated by spaces:

Code snippet

const array = ['Hello', 'World'];
const joinedArray = array.join(' ');

console.log(joinedArray); // "Hello World"
Enter fullscreen mode Exit fullscreen mode

- Array.flat:

The Array.flat() method in JavaScript is used to flatten an array. This means that it will combine all of the sub-arrays in the original array into a single, flat array.

The syntax for the Array.flat() method is:

Code snippet

array.flat([depth])
Enter fullscreen mode Exit fullscreen mode

Where:

  • array is the array to be flattened
  • depth is an optional parameter that specifies the maximum depth of the sub-arrays to be flattened. If no depth is specified, all sub-arrays will be flattened.

For example, the following code will flatten the array variable:

Code snippet

const array = [1, 2, [3, 4], [5, 6]];
const flatArray = array.flat();

console.log(flatArray); // [1, 2, 3, 4, 5, 6]

Enter fullscreen mode Exit fullscreen mode

Array.find:

The Array.find() method in JavaScript is used to find the first element in an array that satisfies a given condition. The method takes a callback function as an argument, and the callback function is called once for each element in the array. If the callback function returns a truthy value for any element, the Array.find() method returns that element. If the callback function never returns a truthy value, the Array.find() method returns undefined.

The syntax for the Array.find() method is:

Code snippet

array.find(callback)
Enter fullscreen mode Exit fullscreen mode

Where:

  • array is the array to be searched
  • callback is a function that takes an element of the array as its argument and returns a truthy or falsy value

For example, the following code will find the first element in the array variable that is greater than 10:

Code snippet

const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const firstGreaterThan10 = array.find(element => element > 10);

console.log(firstGreaterThan10); // 11
Enter fullscreen mode Exit fullscreen mode

- Array.indexOf:

The array.indexOf() method in JavaScript returns the first index of the element in the array if it is present at least once. Returns -1 if the element is not found in the array.

The syntax for array.indexOf() is:

Code snippet

array.indexOf(element[, fromIndex])
Enter fullscreen mode Exit fullscreen mode
  • element is the value to search for.
  • fromIndex is the index to start the search from. If not specified, the search starts at index 0.

Code snippet

const array = [1, 2, 3, 4, 5];

// Find the index of the first occurrence of 3
const index = array.indexOf(3);
console.log(index); // 2

// Find the index of the first occurrence of 6
const index = array.indexOf(6);
console.log(index); // -1

// Find the index of the first occurrence of 3, starting at index 2
const index = array.indexOf(3, 2);
console.log(index); // 3

Enter fullscreen mode Exit fullscreen mode

- Array.includes:

The Array.prototype.includes() method in JavaScript checks if an array contains a specified element or not.

The syntax for Array.prototype.includes() is:

  • element is the value to search for.
  • fromIndex is the index to start the search from. If not specified, the search starts at index 0.

The includes() method returns true if the element is found in the array, and false if it is not found.

Here are some examples of how to use Array.prototype.includes():

Code snippet

const array = [1, 2, 3, 4, 5];

// Check if the array includes 3
const includes3 = array.includes(3);
console.log(includes3); // true

// Check if the array includes 6
const includes6 = array.includes(6);
console.log(includes6); // false

// Check if the array includes 3, starting at index 2
const includes3FromIndex2 = array.includes(3, 2);
console.log(includes3FromIndex2); // true
Enter fullscreen mode Exit fullscreen mode

- Array.findIndex:

The Array.findIndex() method in JavaScript is used to find the index of the first element in an array that satisfies a provided testing function. The method returns the index of the element that satisfies the testing function or -1 if no element passed the test.

The syntax for the Array.findIndex() method is as follows:

Code snippet

array.findIndex(callback[, thisArg])
Enter fullscreen mode Exit fullscreen mode
  • array is the array to be searched.
  • callback is a function that takes an element of the array as its input and returns a Boolean value. The function will be called for each element in the array, and the first time it returns a truthy value, the Array.findIndex() method will return the index of that element.
  • thisArg is an optional value that will be used as the this value inside the callback function. If thisArg is not specified, the this value will be undefined.

Here is an example of how to use the Array.findIndex() method:

Code snippet

const arr = [1, 2, 3, 4, 5];

const index = arr.findIndex(x => x % 2 === 0);

console.log(index); // 1
Enter fullscreen mode Exit fullscreen mode

- Array.forEach:

The Array.forEach() method is an iterative method that calls a provided callback function once for each element in an array in ascending-index order. Unlike map(), forEach() always returns undefined and is not chainable. The typical use case is to execute side effects at the end of a chain.

The callback function is called with the following arguments:

  • element: The current element being processed in the array.
  • index: The index of the current element being processed in the array.
  • array: The array forEach() was called upon.

The callback function can modify the array, but it cannot change the length of the array.

Here is an example of how to use Array.forEach():

Code snippet

const array = [1, 2, 3, 4, 5];

array.forEach((element, index) => {
  console.log(`The element at index ${index} is ${element}`);
});
Enter fullscreen mode Exit fullscreen mode

The Array.forEach() method is an iterative method that calls a provided callback function once for each element in an array in ascending-index order. Unlike map(), forEach() always returns undefined and is not chainable. The typical use case is to execute side effects at the end of a chain.

The callback function is called with the following arguments:

  • element: The current element being processed in the array.
  • index: The index of the current element being processed in the array.
  • array: The array forEach() was called upon.

The callback function can modify the array, but it cannot change the length of the array.

Here is an example of how to use Array.forEach():

Code snippet

const array = [1, 2, 3, 4, 5];

array.forEach((element, index) => {
  console.log(`The element at index ${index} is ${element}`);
});

Enter fullscreen mode Exit fullscreen mode

Use code with caution. Learn more

content_copy

This code will print the following to the console:

Code snippet

The element at index 0 is 1
The element at index 1 is 2
The element at index 2 is 3
The element at index 3 is 4
The element at index 4 is 5

Enter fullscreen mode Exit fullscreen mode

- Array.map:

The Array.map() method is an iterative method that creates a new array from the results of calling a provided function on every element in the calling array. The callback function is called with the following arguments:

  • element: The current element being processed in the array.
  • index: The index of the current element being processed in the array.
  • array: The array map() was called upon.

The callback function must return a value, which will be the corresponding element in the new array.

Here is an example of how to use Array.map():

Code snippet

const array = [1, 2, 3, 4, 5];

const newArray = array.map((element) => {
  return element * 2;
});

console.log(newArray); // [2, 4, 6, 8, 10]
Enter fullscreen mode Exit fullscreen mode

- Array.filter:

The Array.filter() method is an iterative method that creates a new array from the elements of an existing array that pass a test implemented by a provided function. The callback function is called with the following arguments:

  • element: The current element being processed in the array.
  • index: The index of the current element being processed in the array.
  • array: The array filter() was called upon.

The callback function must return a Boolean value, which will be used to determine whether the element is included in the new array. If the callback function returns true, the element is included in the new array. If the callback function returns false, the element is not included in the new array.

Here is an example of how to use Array.filter():

Code snippet

const array = [1, 2, 3, 4, 5];

const newArray = array.filter((element) => {
  return element % 2 === 0;
});

console.log(newArray); // [2, 4]
Enter fullscreen mode Exit fullscreen mode

- Array.reduce:

The Array.reduce() method is a recursive method that applies a function to each element of an array, from left to right, and returns a single value. The callback function is called with the following arguments:

  • accumulator: The current value of the accumulator.
  • element: The current element being processed in the array.
  • index: The index of the current element being processed in the array.
  • array: The array reduce() was called upon.

The callback function must return a value, which will be the new value of the accumulator.

Here is an example of how to use Array.reduce():

const array = [1, 2, 3, 4, 5];

const sum = array.reduce((accumulator, element) => {
  return accumulator + element;
}, 0);

console.log(sum); // 15
Enter fullscreen mode Exit fullscreen mode

- Array.sort:

The Array.sort() method in JavaScript is used to sort the elements of an array in place. By default, the sort() method sorts the array elements in ascending order with the smallest value first and largest value last. The sort() method casts elements to strings and compares the strings to determine the orders.

The following is an example of how to use the Array.sort() method to sort an array of numbers in ascending order:

Code snippet

const numbers = [10, 5, 2, 1, 8, 7, 6, 3, 4, 9];

numbers.sort();

console.log(numbers); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Enter fullscreen mode Exit fullscreen mode

- Array methods chaining:

JavaScript chaining is a technique that allows you to call multiple methods on an object in a single statement. This can make your code more concise and easier to read.

To chain methods, you simply separate the method names with periods (.). For example, the following code chains the toUpperCase() and slice() methods on the str variable:

Code snippet

str.toUpperCase().slice(0, 5);
Enter fullscreen mode Exit fullscreen mode

This code will return the first five characters of str in uppercase letters.

- String methods - add codeblocks and mention mutable/immutable;

Mutable and Immutable Methods (in strings);

In JavaScript, strings are immutable, which means that once a string is created, its value cannot be changed. This is in contrast to mutable objects, such as arrays, which can be changed after they are created.

There are a number of methods in JavaScript that can be used to manipulate strings. Some of these methods are mutable, while others are immutable.

Mutable methods

  • concat(): This method concatenates the specified string to the end of the current string.
  • replace(): This method replaces all occurrences of the specified substring with the specified replacement string.
  • slice(): This method returns a new string that is a substring of the current string.

Immutable methods

  • toUpperCase(): This method returns a new string that is the uppercase version of the current string.
  • toLowerCase(): This method returns a new string that is the lowercase version of the current string.
  • trim(): This method returns a new string that has all leading and trailing whitespace characters removed.

It is important to be aware of the difference between mutable and immutable methods when working with strings. If you are using a mutable method, you should be careful not to modify the string in a way that could affect other parts of your code.

  • toLocaleUpperCase() : Converts a string to uppercase, based on the current locale.

Code snippet

const str = "Hello";
const uppercaseStr = str.toLocaleUpperCase(); // "HELLO"
Enter fullscreen mode Exit fullscreen mode

const str = "Hello";
const repeatedStr = str.repeat(3); // "HelloHelloHello"

  • trim() : Removes whitespace from the beginning and end of a string.

Code snippet

const str = "   Hello, world!   ";
const trimmedStr = str.trim(); // "Hello, world!"
Enter fullscreen mode Exit fullscreen mode
  • substring() : Extracts a substring from a string.

Code snippet

const str = "Hello, world!";
const substring = str.substring(0, 5); // "Hello"
Enter fullscreen mode Exit fullscreen mode
  • split() : Splits a string into an array of substrings based on a delimiter.

Code snippet

const str = "Hello, world!";
const arrayOfSubstrings = str.split(","); // ["Hello", "world!"]
Enter fullscreen mode Exit fullscreen mode
  • ** slice()** : Extracts a substring from a string.

Code snippet

const str = "Hello, world!";
const substring = str.slice(0, 5); // "Hello"
Enter fullscreen mode Exit fullscreen mode
  • replace() : Replaces all occurrences of a substring in a string with another string.

Code snippet

const str = "Hello, world!";
const newStr = str.replace("world", "universe"); // "Hello, universe!"
Enter fullscreen mode Exit fullscreen mode
  • indexOf() : Returns the index of the first occurrence of a substring in a string.

Code snippet

const str = "Hello, world!";
const index = str.indexOf("world"); // 6
Enter fullscreen mode Exit fullscreen mode
  • concat() : Concatenates two or more strings.

Code snippet

const str1 = "Hello, ";
const str2 = "world!";
const combinedStr = str1.concat(str2); // "Hello, world!"
Enter fullscreen mode Exit fullscreen mode

- Object methods and operations - add codeblocks and mention mutable/immutable:

  • keys() : Returns an array of the object's property names.

Code snippet

const obj = {
  name: "John Doe",
  age: 30
};

const propertyNames = Object.keys(obj); 
Enter fullscreen mode Exit fullscreen mode
  • values() : Returns an array of the object's property values.

Code snippet

const obj = {
  name: "John Doe",
  age: 30
};

const propertyValues = Object.values(obj); // ["John Doe", 30]
Enter fullscreen mode Exit fullscreen mode
  • entries() : Returns an array of tuples, where each tuple contains a property name and its value.

Code snippet

const obj = {
  name: "John Doe",
  age: 30
};

const propertyEntries = Object.entries(obj); // [["name", "John Doe"], ["age", 30]]
Enter fullscreen mode Exit fullscreen mode
  • get() : Returns the value of the object's property with the specified name.

Code snippet

const obj = {
  name: "John Doe",
  age: 30
};

const name = obj.get("name"); // "John Doe"
Enter fullscreen mode Exit fullscreen mode
  • set() : Sets the value of the object's property with the specified name.

Code snippet

const obj = {
  name: "John Doe"
};

obj.set("age", 30);

const age = obj.get("age"); // 30
Enter fullscreen mode Exit fullscreen mode
  • deleteProperty() : Deletes the object's property with the specified name.

Code snippet

const obj = {
  name: "John Doe"
};

delete obj.name;

console.log(obj.name); // undefined
Enter fullscreen mode Exit fullscreen mode
  • toString() : Returns a string representation of the object.

Code snippet

const obj = {
  name: "John Doe",
  age: 30
};

const stringRepresentation = obj.toString(); // "{name: 'John Doe', age: 30}"
Enter fullscreen mode Exit fullscreen mode

Hoisting in JS:

Sure, hoisting in JavaScript is the process of moving all variable and function declarations to the top of their scope, before any code is executed. This means that even if you declare a variable or function below where it is used, it will still be available in the scope where it is used.

For example, the following code will print "Hello, world!" even though the greet() function is declared below the console.log() statement:

Code snippet

function greet() {
  console.log("Hello, world!");
}

greet();
Enter fullscreen mode Exit fullscreen mode

This is because the greet() function is hoisted to the top of the scope where it is defined.

Hoisting can be a bit confusing at first, but it is an important concept to understand in JavaScript. By understanding hoisting, you can avoid errors in your code and write more efficient code.

Here are some things to keep in mind about hoisting:

  • Only variable and function declarations are hoisted. Expressions are not hoisted.
  • Hoisting does not affect the order of execution of your code. The code will still be executed in the order in which it is written.
  • Hoisting can be used to your advantage. For example, you can use hoisting to declare variables that are only used in one part of your code. This can make your code more readable and easier to maintain.

Scopes:

In JavaScript, a scope is a region of code where a variable is accessible. There are three types of scopes in JavaScript:

  • Global scope: The global scope is the outermost scope in a JavaScript program. Variables declared in the global scope are accessible from anywhere in the program.
  • Function scope: A function scope is created when a function is defined. Variables declared in a function scope are only accessible from within that function.
  • Block scope: A block scope is created when a block of code is enclosed in curly braces ({}). Variables declared in a block scope are only accessible from within that block.

When you declare a variable, the JavaScript engine will create a new entry in the current scope's symbol table. The symbol table is a data structure that stores information about all of the variables in a scope. When you access a variable, the JavaScript engine will look up the variable's name in the current scope's symbol table. If the variable is found, the engine will return the variable's value. If the variable is not found, the engine will throw an error.

It is important to understand scopes in JavaScript in order to avoid errors in your code. For example, if you declare a variable in a function scope and then try to access it outside of the function, you will get an error.

Here are some tips for using scopes in JavaScript:

  • Declare variables in the smallest scope possible. This will make your code more readable and easier to maintain.
  • Use descriptive variable names. This will help you to understand what each variable is used for.
  • Use the let and const keywords to declare variables. These keywords will help you to avoid errors caused by accidentally overwriting variables.

Closures:

A closure is a function that has access to variables that are declared in an outer scope. This means that the function can access those variables even after the outer scope has closed. Closures are a powerful tool that can be used to create reusable code and to implement complex functionality.

To create a closure, you need to define a function inside of another function. The inner function will have access to all of the variables that are declared in the outer function, even after the outer function has returned.

For example, the following code defines a function called makeCounter() that returns a function that increments a counter. The makeCounter() function creates a closure around the counter variable. This means that the increment() function will be able to access the counter variable even after the makeCounter() function has returned.

Code snippet

function makeCounter() {
  let counter = 0;

  function increment() {
    counter++;
    return counter;
  }

  return increment;
}

const counter = makeCounter();

console.log(counter()); // 1
console.log(counter()); // 2
Enter fullscreen mode Exit fullscreen mode

In this example, the increment() function is able to access the counter variable because it is defined inside of the makeCounter() function. The makeCounter() function returns the increment() function, which means that the increment() function can be used outside of the makeCounter() function.

Closures can be used to create reusable code. For example, you could define a function that takes a callback function as an argument. The callback function would then be able to access any variables that are declared in the outer function. This allows you to encapsulate functionality in a function and then reuse that functionality in other parts of your code.

Closures can also be used to implement complex functionality. For example, you could use closures to create a timer function that keeps track of the time that has passed since the function was called. The timer function could then be used to implement features such as a countdown timer or a stopwatch.

Closures are a powerful tool that can be used to create reusable code and to implement complex functionality. If you are not familiar with closures, I encourage you to learn more about them. They can be a valuable asset in your JavaScript toolkit.

- Debugging:

Debugging in JavaScript can be a challenge, but there are a number of tools and techniques that can help you find and fix errors in your code.

One of the most basic debugging techniques is to use the console.log() function to print the values of variables and expressions to the console. This can be helpful for tracking down the source of an error.

Another useful debugging technique is to use breakpoints. Breakpoints allow you to pause the execution of your code at a specific point. This can be helpful for inspecting the values of variables and expressions at a specific point in the code.

There are a number of different debugging tools available for JavaScript. Some popular debugging tools include:

  • Chrome DevTools: Chrome DevTools is a powerful debugging tool that is built into the Chrome web browser. Chrome DevTools allows you to inspect the DOM, the JavaScript code, and the network traffic for a web page.

Chrome DevTools in Google ChromeOpens in a new windowChrome Developers

Chrome DevTools in Google Chrome

  • Node Inspector: Node Inspector is a debugging tool for Node.js applications. Node Inspector allows you to inspect the JavaScript code, the environment variables, and the system logs for a Node.js application.

Node Inspector in Node.jsOpens in a new windowGeeksforGeeks

Node Inspector in Node.js

  • WebStorm: WebStorm is a commercial IDE for JavaScript development. WebStorm includes a powerful debugger that allows you to inspect the JavaScript code, the DOM, and the network traffic for a web page.

WebStorm IDEOpens in a new windowJetBrains

WebStorm IDE

No matter what debugging tool you choose, the most important thing is to be patient and persistent. Debugging can be a time-consuming process, but it is essential for finding and fixing errors in your code.

Here are some additional tips for debugging JavaScript code:

  • Start by isolating the problem. Try to narrow down the problem to a specific part of your code. This can be done by using breakpoints and the console.log() function.
  • Use the debugger's features to inspect the code. The debugger's features can be used to inspect the values of variables, the structure of the code, and the execution of the code.
  • Be patient and persistent. Debugging can be a time-consuming process, but it is essential for finding and fixing errors in your code.

Top comments (0)