Basic JavaScript Interview Questions and Answers

Basic JavaScript Interview Questions and Answers

JavaScript interview questions.

JavaScript is widely used programming language which is used by web developers. JavaScript is supports all major browsers.

if you are looking for a web developer job then, you will most likely be asked to answer to some questions about JavaScript Questions.

I Hope this article is helpful for you to prepare interview for javascript.

1. Difference Between Java and JavaScript ?

  • Java is strongly typed language but JavaScript is loosely typed language.
  • JavaScript is Object Oriented Programming language while JavaScript is Object based Programming language.
  • Java applications can run in any virtual machine but JavaScript can run only in browsers.
  • Java is mainly used for backend in web-development while JavaScript is used for frontend and backend both.

2. Data types in JavaScript ?

  • Boolean [ true or false ]
  • null, undefined [ are types that are values in and of themselves. ]
  • Numbers [ stored as number of 64-bits ]
  • Strings [ set or collection of characters ]
  • Object [ Collection of properties ]
  • BigInt [ use to precisely represent large number ]

3. Difference between "==" and "===" ?

  • Both operators are used for a comparison in JavaScript.
  • The "==" operator compare two values and return true if both value are same.
  • but, "===" operator compare two values strictly and return true if both values are same with datatype.
  • Example : ``` const a = 10; const b = "10";

if(a==b) //it return true

if(a===b)//it return false because of strictly comparing with datatype.


## 4. is JavaScript is case-sensitive language ?
- Yes, JavaScript is Case-Sensitive Language, this means JavaScript recognizes difference between uppercase and lowercase.
- Example :

const a = 10; const A = 20

console.log(a) //it prints 10 console.log(A) //it prints 20


## 5. How can you create Object in JavaScript ?
- There are at least three ways to create object using different methods.
- Object literals :

const person = { name : "Akshar", age : 20, active : true }

- Constructor Function :

function person(name, age, active){ this.name = name, this.age = age, this.active = active }

const obj = new person("Akshar", 20, true)

- Object.create :

const obj = Object.create({ name : "Akshar", age : 20, active : true })

console.log(obj.name) //Akshar


##6. Difference between let and var ? 
- JavaScript, the keywords let and var can be used to declare variables. However, there are some essential differences between these two keywords. Variables declared with var are accessible anywhere within their containing function, including the lines before the variable declaration and possible outer block scopes in function. In contrast, variables declared with let are only accessible within the block in which they were declared. This can be useful for preventing variable collisions or for creating private variables.
- In addition, variables declared with let are hoisted to the top of their containing scope in the same way as variables declared with var, but while accessing a var before it’s defined will evaluate to undefined, let will throw a ReferenceError (Temporal Dead Zone). As a result, let can be used to help avoid unexpected behavior when accessing variables before they have been initialized. For these reasons, it is generally considered best practice to use let when declaring variables in JavaScript.

## 7. What is const in JavaScript ? 
- const is keyword like let and var in JavaScript. but a variable created with const can't be reassigned.

const a = 10; a = 20; // it throws error [ TypeError ] ```

8. How can you create array in JavaScript ?

  • Creating array in JavaScript is Simple. Here i mentioned 3 methods for creating arrays :
  • using Array constructor : const array = new Array(1,2,3);
  • Array Constructor and push method : const array = new Array(); array.push(1);array.push(2);
  • using array literals : const array = [1,2,3,4];

9. What is callback in JavaScript ?

  • A callback is a function that passed as an argument to another function.
  • The callback function is invoked by other function. Callbacks are used for inversion of control, meaning that it's not you who decide when and how exactly your logic will be run, but by the library or framework.

10. What is Closure in JavaScript ?

  • In JavaScript, you can nest functions within functions. Variables declared in the outer function are available to the inner function, even if the inner one is executed later in time. Thus the outer function’s variables are part of the inner function’s closure.

11. What is memoization in JavaScript ?

  • Memoization is an optimization technique used to speed up programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again. When a memoized function is called with the same arguments, the previous result is simply looked up and returned, without needing to re-execute the entire function. While memoization is a powerful optimization tool, it is important to use it sparingly, as it can lead to increased memory usage and code that is difficult to understand.

12. What are classes in JavaScript ?

  • Classes in JavaScript are template for creating objects.
  • A class definition can specify the kind of data that an object instance of that class type will contain, and it can also specify the methods (functions) that can be invoked on instances of that type. However, classes in JavaScript are merely a syntactic sugar over constructor functions.

If this is useful for you, please like, share and Follow 👍

Did you find this article valuable?

Support Javascript Blogs by becoming a sponsor. Any amount is appreciated!