Javascript Interview
Javascript Interview
Answer: It allows you to design a flexible responsive layout structure without using any float or
positioning property of CSS. To use CSS flexbox you need to define a flex container initially.
img {
opacity: 0.6;
filter: alpha(opacity=60); /* For IE8 and earlier */}
Answer : Doctype: A doctype or document type declaration is an instruction that tells the web browser
about the markup language in which the current page is written. ... In HTML 5, the DOCTYPE
declaration is only required for enabling the standard mode for writing documents.
Answer : Both are comparison operators. The difference between both the operators is that,“==” is
used to compare values whereas, “ === “ is used to compare both value and types.
Example:
(x === y) // Returns false since the typeof x is "number" and typeof y is "string"
In general terms, the scope will let us know at a given part of code, what are the variables and functions
that we can or cannot access.
Global Scope
Local or Function Scope
Block Scope
Global Scope
Variables or functions declared in the global namespace have global scope, which means all the
variables and functions having global scope can be accessed from anywhere inside the code
Function Scope
Any variables or functions declared inside a function have local/function scope, which means that all
the variables and functions declared inside a function, can be accessed from within the function and not
outside of it.
function awesomeFunction(){
var a = 2 ;
Block Scope
Block scope is related to the variables declared using let and const. Variables declared with var do not
have block scope.
Block scope tells us that any variable declared inside a block { }, can be accessed only inside that block
and cannot be accessed outside of it.
{
let x = 45 ;
}
Scope Chain
var y = 24 ;
function favFunction(){
var x = 667 ;
var anotherFavFunction = function (){
console.log(x); // Does not find x inside anotherFavFunction, so looks for
variable inside favFunction, outputs 667
}
anotherFavFunction();
yetAnotherFavFunction();
}
favFunction();
As you can see in the code above, if the javascript engine does not find the variable in local scope, it
tries to check for the variable in the outer scope. If the variable does not exist in the outer scope, it tries
to find the variable in the global scope.
If the variable is not found in the global space as well, reference error is thrown.
In javascript, functions are treated as first-class citizens, they can be used as an argument of another
function, can be returned by another function and can be used as a property of an object.
Functions that are used as an argument to another function are called callback functions.
Example:
function divideByHalf(sum){
console.log( Math .floor(sum / 2 ));
}
function multiplyBy2(sum){
console.log(sum * 2 );
}
function operationOnSum(num1,num2,operation){
var sum = num1 + num2;
operation(sum);
}
In the code above, we are performing mathematical operations on the sum of two numbers.
The operationOnSum function takes 3 arguments, first number, second number, and the operation that
is to be performed on their sum (callback) .
Both divideByHalf and multiplyBy2 functions are used as callback functions in the code above.
These callback functions will be executed only after the function operationOnSum is executed.
Therefore, callback is a function that will be executed after another function gets executed.
=================================================================
Answer : The spread operator allows us to spread the value of an array (or any iterable) across zero or
more arguments in a function or elements in an array (or any iterable). The rest parameter allows us to
pass an indefinite number of parameters to a function and access them in an array.
================================================================
Answer : push
Pop
Splice
indexOf
Slice
Length
Filter
=================================================================
Answe : The main difference between constructor and ngOnInit is that ngOnInit is lifecycle hook and
runs after constructor. Component interpolated template and input initial values aren't available in
constructor, but they are available in ngOnInit . The practical difference is how ngOnInit affects how
the code is structured
=============================================================
10) What is Directives???
Answer : Directives are custom HTML attributes which tell angular to change the style or behavior of
the Dom elements.
When we say that components are the building blocks of Angular applications, we are actually saying
that directives are the building blocks of Angular applications.
Types:
Components — directives with a template.
Structural directives — change the DOM layout by adding and removing DOM elements.
Attribute directives — change the appearance or behavior of an element, component, or another
directive.