JavaScript Interview Questions

Q:Is Js strongly typed language?
Ans: No its dynamically typed language.

Q:Js supports function overloading?
Ans:There is no real function overloading in JavaScript since it allows to pass any number of parameters of any type. We have to check inside the function how many arguments have been passed and what type they are.

Q:Have add function with two parameters and call with only one parameter
Ans:

Q:What is the difference between == and ===?
Ans :
==    only compares values.
===  compares values and its type.
JavaScript has both strict and type converting comparisons. A strict comparison (===) is only true if the operands are of the same type and the contents match.
The more commonly-used abstract comparison (==) converts the operands to the same type before making the comparison. For relational abstract comparisons, the operands are first converted to primitives, then to the same type, before comparison.

Q.What is Hoisting?
Ans:

  • One of the advantages of JavaScript putting function declarations into memory before it executes any code segment is that it allows you to use a function before you declare it in your code. 
  • In other words, a variable can be used before it has been declared.
  • However in contrast, undeclared variables do not exist until code assigning them is executed.
  • Therefore, assigning a value to an undeclared variable implicitly creates it as a global variable when the assignment is executed.
  • This means that, all undeclared variables are global variables.
  • Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution.

Q.What are the primitive data types in JavaScript?
Ans:The primitive data types in JavaScript are

Boolean,

  • number
  • string
  • null
  • undefined
  • nan

Q.What is NAN?
Ans:

  • The NAN is a global property whose value represents “NOT A NUMBER”.
  • NaN always compares unequal to any number, including NaN, it is usually used to indicate an error condition for a function that should return a valid number.
  • Use the isNaN() global function to see if a value is an NaN value.

Q.var vs let
Ans:var

  •  In Javascript if we define a variable with key word var then that variable is available in complete function even if is defined in block like if or
  • for.The var statement declares a variable, optionally initializing it to a value.Assigning a value to an undeclared variable implicitly creates it as a
  • global variable (it becomes a property of the global object) when the assignment is executed.

let

  • In Javascript if we define a variable with key word let then that variable is not available in complete function.
  • The let statement declares a block scope local variable, optionally initializing it to a value.

Q.Function scope vs block scope
Ans: In Javascript if we define a variable with key word var then that variable is available in complete function even if is defined in block like if or for.The var statement declares a variable, optionally initializing it to a value.Assigning a value to an undeclared variable implicitly creates it as a global variable (it becomes a property of the global object) when the assignment is executed.The variables inside the function scope is always accessible out of the scope.
In Javascript if we define a variable with key word let then that variable is not available in complete function.The let statement declares a block scope local variable, optionally initializing it to a value.Example:

The output of the above file will show error as not defined because let is accessible only within the scope. So, if you try to call or invoke it out of the scope it will show error.

Q.What are immediately invoked function?
Ans:In Javascript we can invoke the function immediately as part of the declaration itself with special syntax.They are called as Immediately Invoked functions are Self Invoked Functions.

Q. What is Block vs Inline vs Inline-block element?
Ans: Block element will always come in new line and has height,width and border other properties. Inline elements will come in same line if there is available width don’t have height,width etc properties. Inline Block elements will come in same line if there is available width but honour the width,height,border and other properties.
.

Q:In below code which one will be alerted

  1. 20
  2. 1

Ans : 20

Explanation : In a function if there is a local variable is available with same name as global variable it will always look for the local variable in this case x. in this case x is declared locally inside the block . the variable declaration is hoisted to top  when the function execution starts .  x will undefined when the execution starts and !x  is true it goes inside and the value gets updated with the 20. if we declare variable with var  it will be available in the complete function scope , hence when we alert even after the block it will be 20.

 

Q: what will be printed in the below code

  1. 20
  2. 5
  3. undefined

Ans : undefined

Explanation : Inside printA function we have local variable with name “a”, hence it looks for the local variable instead global variable. Though variable declaration is hoisted value for a is not assigned at the time of printing it to the console. Hence it will print as undefined.

Angular Interview Questions

Q:In Component Class which one will execute first?

a)constructor

b)ngOnInit
Ans: constructor

Q:In Component Class which one will execute first?

a)ngOnInit

b)ngOnChanges
Ans:ngOnInit

 

Leave a Reply

Your email address will not be published. Required fields are marked *