Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Unveiling Javascript Scopes

Posted on Oct 3 • Originally published at arindam1729.hashnode.dev Scopes are one of the fundamental concepts of Javascript. Many Developers face problems understanding the behavior of variables.If you are also facing the same problem.Don't Worry! You are in the right place!In this blog, We'll explore What is Scope, and it's Types with examples.Scope in JavaScript refers to the current context of code, which determines the accessibility of variables to JavaScript.We can only access the variables within their scopes otherwise they will throw an error.Didn't Get it?No worries!Let's understand this with some examples.So what will be the Out of this code?Wondering why it's showing "Villain is not defined" in spite of declaring it?Here comes the Scope.Let me explain to you why this is happening,We have defined the Variable Villain with the function so the scope of the variable is within the function which means we can only access the variable within the function.There are 3 main types of scopes in JavaScript:If the variable is declared outside of a function then it has global scope. It can be accessed from anywhere in your JavaScript code.As the variable is declared globally, Both func1 and func2 can access the value and print Arindam.A variable declared within a function has function scope. It can only be accessed within that function.If you declare a variable with let and const , They are only accessible within the block they are declared in - between curly braces { }.Let's understand this with examples:It throws a reference error because the scope of the variable is within the curly braces.Now let's move to the next example,What will be the output here?Undefined??No, the output will be "Messi" .This is because the Variable defined with the var keyword is not block-scoped.💡Note: Var is 'Function-scoped' whereas let and const are 'Block-scoped'If you found this blog post helpful, please consider sharing it with others who might benefit. You can also follow me for more content on Javascript and other web development topics.To sponsor my work, please visit: Arindam's Sponsor Page and explore the various sponsorship options.Connect with me on Twitter, LinkedIn, Youtube and GitHub.Thank you for Reading :)Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Vincent Tsen - Sep 16 Arnav Chauhan - Sep 15 Daniel Bellmas - Sep 15 François Roget - Sep 15 Once suspended, arindam_1729 will not be able to comment or publish posts until their suspension is removed. Once unsuspended, arindam_1729 will be able to comment and publish posts again. Once unpublished, all posts by arindam_1729 will become hidden and only accessible to themselves. If arindam_1729 is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Arindam Majumder . They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag arindam_1729: arindam_1729 consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging arindam_1729 will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Unveiling Javascript Scopes

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×