Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Basic JavaScript Questions

Please add your answers : -

Q1. How many types of scopes are there in JavaScript?

Q2. Explain closure?

Q3. Is let and const are the var replacement?

1 Answer

Steven Parker
Steven Parker
243,656 Points
  1. "Function" scope includes the code inside a function, "block" scope includes the code between braces, and "global" scope is the entire program.
  2. A "closure" is the scope of a function defined within another function. It has access to the variables defined in the outer function, but it has variables and/or functions of its own which are private.
  3. "Let", "const" and "var" are all declaration keywords, with slight differences. "Var" has function scope, while "let" and "const" have block scope. And "const" additionally prevents any changes after initialization. The purpose of a variable will determine which declaration is the most appropriate for it.

Steven Parker, As I know there is only one type of scope in JavaScript which is lexical scope and block, function and global scope are the part of it. If I'm wrong let me know.

Steven Parker
Steven Parker
243,656 Points

Perhaps I misunderstood what you meant by "type". Yes, JavaScript has only lexical (or "static") scope.