Software Development Exam  >  Software Development Notes  >  JavaScript for Web Development  >  Assignment: JavaScript Scope and Hoisting

Assignment: JavaScript Scope and Hoisting | JavaScript for Web Development - Software Development PDF Download

Multiple Choice Questions (MCQs)

Q.1. Which of the following best describes the concept of scope in JavaScript?
(a) 
The visibility or accessibility of variables, functions, and objects in some particular part of your code
(b) The order in which statements are executed in a JavaScript program
(c) The process of organizing and structuring code into smaller, reusable components
(d) The ability to assign multiple values to a single variable

Ans. (a)

Q.2. What is the scope of a variable declared with the var keyword inside a function?
(a)
Global scope
(b) Local scope
(c) Block scope
(d) Function scope

Ans. (d)

Q.3. Hoisting in JavaScript refers to:
(a) 
The process of raising an error when an undeclared variable is accessed
(b) The process of moving variable and function declarations to the top of their containing scope
(c) The process of assigning a default value to all variables
(d) The process of automatically converting all variables to the global scope

Ans. (b)

Q.4. Which of the following statements about hoisting is true?
(a) 
Only variable declarations are hoisted, not their assignments
(b) Both variable declarations and assignments are hoisted
(c) Only variable assignments are hoisted, not their declarations
(d) Hoisting only applies to variables declared with the let keyword

Ans. (a)

Q.5. The "let" and "const" keywords introduced in ES6 have which type of scope?
(a) 
Global scope
(b) Local scope
(c) Block scope
(d) Function scope

Ans. (c)

Higher Order Thinking Questions (HOTS)

Q.1. Explain the difference between function scope and block scope in JavaScript.

Function scope refers to the visibility of variables declared within a function, while block scope refers to the visibility of variables declared within a block of code (usually delimited by curly braces '{}').

Q.2. Consider the following code snippet:
var x = 10;

function foo() {

  console.log(x);

  var x = 20;

}

foo();
What will be the output of the above code? Explain the reason behind the output.

The output of the code will be 'undefined'. The reason is that when the 'console.log(x)' statement is executed inside the 'foo()' function, the variable 'x' is hoisted to the top of the function scope but not initialized with a value. Therefore, it has the value 'undefined' at that point.

Q.3. What is the output of the following code?
let x = 10;

if (true) {

  let x = 20;

  console.log(x);

}

console.log(x);

The output of the code will be:
20

10

The first 'console.log(x)' statement inside the if block outputs '20' because 'x' is assigned the value '20' within that block. The second 'console.log(x)' statement outputs '10' because it refers to the 'x' variable declared in the outer scope.

Q.4. Explain the concept of lexical scoping in JavaScript.

Lexical scoping in JavaScript means that the scope of a variable is determined by its position within the source code's lexical structure, or in other words, its location in the code. Variables declared outside of any function have global scope, while variables declared inside a function have function scope.

Q.5. What is the difference between 'var', 'let', and 'const' in terms of hoisting?

The 'var' keyword hoists both variable declarations and assignments, meaning that the variable is accessible throughout its containing scope. On the other hand, 'let' and 'const' variables are also hoisted but not initialized. They are in a "temporal dead zone" until they are declared and assigned in the code.

Fill in the Blanks

1. The _________ scope in JavaScript refers to variables declared outside any function or block.

Global

2. Hoisting in JavaScript moves variable and function _________ to the top of their containing scope.

declarations

3. The "let" and "const" keywords introduced in ES6 have _________ scope.

block

4. Lexical scoping determines the _________ of a variable based on its position within the source code.

scope

5. The _________ keyword is used to declare a block-scoped variable in JavaScript.

'let'

True/False

1. In JavaScript, a variable declared with 'var' keyword is hoisted to the top of its scope.

True

2. Variables declared with 'let' or 'const' keywords are not hoisted in JavaScript.

True

3. In JavaScript, a variable declared inside a function has function scope.

True

4. Block scope was introduced in JavaScript before the ES6 version.

False

5. Hoisting applies only to variable declarations and not to function declarations.

False

Hands-On Questions

Q.1. Write a JavaScript function called 'multiplyByTwo' that takes a number as an argument and returns the result of multiplying that number by 2.

function multiplyByTwo(number) {

  return number * 2;

}

Q.2. Write a JavaScript code snippet that demonstrates block scope and hoisting.

{

  console.log(x);  // ReferenceError: x is not defined

  let x = 10;

  console.log(x);  // 10

}

Q.3. Fix the following code to correctly log the value of 'x' outside the if statement:

let x = 10;

if (true) {

  let x = 20;

  console.log(x);

}

console.log(x);

let x = 10;


if (true) {

  let x = 20;

  console.log(x);  // 20

}


console.log(x);  // 10

Q.4. Write a JavaScript function called 'add' that takes two numbers as arguments and returns their sum. Use the 'let' keyword to declare variables inside the function.

function add(a, b) {

  let sum = a + b;

  return sum;

}

Q.5. Write a JavaScript code snippet that demonstrates lexical scoping and the use of 'var' keyword.

function outer() {

  var x = 10;


  function inner() {

    console.log(x);

  }


  inner();

}


outer();  // Output: 10

Note: The variable 'x' in the 'inner()' function is accessible due to lexical scoping.

The document Assignment: JavaScript Scope and Hoisting | JavaScript for Web Development - Software Development is a part of the Software Development Course JavaScript for Web Development.
All you need of Software Development at this link: Software Development
51 videos|28 docs|12 tests

Top Courses for Software Development

51 videos|28 docs|12 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

past year papers

,

study material

,

video lectures

,

Exam

,

Semester Notes

,

shortcuts and tricks

,

pdf

,

MCQs

,

Important questions

,

Assignment: JavaScript Scope and Hoisting | JavaScript for Web Development - Software Development

,

Previous Year Questions with Solutions

,

Viva Questions

,

Free

,

Sample Paper

,

Assignment: JavaScript Scope and Hoisting | JavaScript for Web Development - Software Development

,

Assignment: JavaScript Scope and Hoisting | JavaScript for Web Development - Software Development

,

mock tests for examination

,

Objective type Questions

,

Summary

,

ppt

,

practice quizzes

,

Extra Questions

;