JavaScript Interview Questions and Answers for Freshers Section - 2

JavaScript Interview Questions and Answers for Freshers Section - 2

·

3 min read

In our previous article about JavaScript Interview Questions and Answers for Freshers, Where we discussed some of the basic interview questions of JavaScript, In this article, we will continue to see some of the common questions of JavaScript.

1. What are the variable scopes in JavaScript?

Scope refers to the current context of code which shows the accessibility of variables to JavaScript. Scopes in Java Script are classified into two types local and global:

Global variables are declared outside of the block

Local variables are declared inside the block

2. Global variable scope :

var creature = "wolf";

variables can be re-assign by using the local scope, we can create new variables with the same name as a variable in an outer scope without changing or reassigning the original value.

In the example below, we will create a global type variable. Within the function is a local variable with the variable name. In console variable value differs depending on the scope and original value is not changed.

Example :

var type = "human";

function transform() {

var type = "werewolf";

console.log(type); }

console.log(type);

transform();

console.log(type);

Output :

human werewolf human

3. Local variable scope?

Local variables are function-scoped Variables declared with the keyword var are always function-scoped, and keywords let and const are block-scoped. It means the new local scope is created from any kind of block, including function blocks, if statements, and for and while loops.

To illustrate the difference between function- and block-scoped variables, I assigned a new variable on the if block using let.

Example :

var fullMoon = true;

let type = "human";

if (fullMoon) {

let type = "werewolf";

console.log(It is a full moon. Lupin is currently a ${type}.); }

console.log(It is not a full moon. Lupin is currently a ${type}.);

Output :

It is a full moon. Lupin is currently a werewolf. It is not a full moon. Lupin is currently a human.

4. What are escape characters?

Escape characters are used while working with special characters like single and double quotes, apostrophes and ampersands. Place \ before the characters to make it display.

Example:

document. write "I m a "good" player."

document. write "I m a \"good\" player."

5. How is DOM utilized in JavaScript?

Document Object Model(DOM) is responsible for how objects in a document interact with each other. The document object model is mandatory in the development of web pages which contain objects like paragraphs, and links and it makes actions of add or delete. The document object model is required to add value-added extra capability to a web page and the use of API gives an advantage over existing models.

6. How to work with es6 Template Literals?

Template literal feature is the ability to include expression variables within a string instead of using concatenation, we can use the ${} syntax to insert a variable.

Example:

const Player = "Rishab Pant";

const Team = " India ";

const favePoem = My favourite cricket player is ${Player} from ${Team}.;

Output :

My favourite cricket player is Rishab Pant from India

7. How does the filter method work on JavaScript Arrays?

The javascript array filter method works in every () method and it tests whether all the elements in the array pass a test implemented by the given function.

Example:

function is below threshold(currentValue)

{

return currentValue < 40;

}

var array1 = [1, 30, 36, 21, 14, 12];

console.log(array1.every(isBelowThreshold));

Output:

True

Conclusion :

The above-mentioned questions are the commonly asked JavaScript Interview Questions and Answers for Freshers. In the Upcoming days, we will also advance the level of javascript Interview Questions and Answers to this blog that is prevalent in the Industry.

Also read our Article:

https://blog.skillsafari.in/javascript-interview-questions-and-answers-for-freshers

Did you find this article valuable?

Support Skill Safari by becoming a sponsor. Any amount is appreciated!