JavaScript Interview Question Cheatsheet

JavaScript Interview Question Cheatsheet

1. Scope:

  • Global Scope: The global scope includes any variable that is not contained within a function or block (a pair of curly braces). Global scope variables can be accessed from anywhere in the program. image.png

  • Local Scope: Variables declared inside a function are local variables. They can only be accessed from within that function; they are not accessible from outside code. image.png

  • Block Scope: Unlike var variables, let and const variables can be scoped to the nearest pair of curly brackets in ES6. They can't be reached from outside that pair of curly braces, which means they can't be accessed from the outside. image.png

2. String:

  • toLowerCase() — This method is used for converting strings to lower case
  • toUpperCase() — This method is used for converting strings to upper case
  • charAt() — This method is used for returning the character at a particular index of a string
  • Concat() — This method is used for concatenating or joining multiple strings into a single string
  • match() — This method is used for retrieving the matches of a string against a pattern string which is provided
  • replace() — This method is used for finding and replacing a given text in the string
  • indexOf() — This method is used for providing the index of the first appearance of a given text inside the string
  • lastIndexOf() — This method is similar to the indexOf() methods and only differs in the fact that it searches for the last occurrence of the character and searches backward
  • slice() — This method is used for extracting an area of the string and returning it
  • split() — This method is used for splitting a string object into an array of strings at a particular index
  • substring() — Even though this method is almost the same as the slice() method but it does not allow negative positions
  • valueOf() — This method is used for returning the primitive value (one without any properties or methods) of a string object

3. Call-Stack:

A call stack is a mechanism for an interpreter (like the JavaScript interpreter in a web browser) to keep track of its place in a script that calls multiple functions — what function is currently being run and what functions are called from within that function, etc. image.png

4. Hoisting:

  • Function Hoisting: Hoisting allows you to use a function before declaring it in your code, as shown in the code snippet given below. Without function hoisting, we would have first to write down the function display, and only then can we call it. image.png

  • Variable Hoisting: You can use a variable in code before it is defined and/or initialized because hoisting works with variables as well. JavaScript, however, only hoists declarations, not initializations! Even if the variable was initially initialized then defined, or declared and initialized on the same line, initialization does not occur until the associated line of code is run. image.png