<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Bashir Salisu Sidi on Medium]]></title>
        <description><![CDATA[Stories by Bashir Salisu Sidi on Medium]]></description>
        <link>https://medium.com/@bashirsalisusidi?source=rss-7ec384964028------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*i-A3oVbwjbT9Pun1OhTwtQ.jpeg</url>
            <title>Stories by Bashir Salisu Sidi on Medium</title>
            <link>https://medium.com/@bashirsalisusidi?source=rss-7ec384964028------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 14:35:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bashirsalisusidi/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[JavaScript Variable and Function SCOPE]]></title>
            <link>https://medium.com/@bashirsalisusidi/javascript-variable-and-function-scope-94d122d1bbab?source=rss-7ec384964028------2</link>
            <guid isPermaLink="false">https://medium.com/p/94d122d1bbab</guid>
            <category><![CDATA[variables]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[functions-in-javascript]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[scopes]]></category>
            <dc:creator><![CDATA[Bashir Salisu Sidi]]></dc:creator>
            <pubDate>Mon, 27 Feb 2023 11:35:22 GMT</pubDate>
            <atom:updated>2023-02-27T11:35:22.448Z</atom:updated>
            <content:encoded><![CDATA[<p>To develop interactive web pages and web apps, JavaScript is a well-liked programming language. It is an adaptable, high-level language that is lively and simple to learn. Variable and function scope are among the basic ideas in JavaScript.</p><p>The term “scope” describes how easily accessing variables and methods are within the code. Variables and functions in JavaScript can have various degrees of scope, which determines how and from where they can be accessed and changed. Writing effective and maintainable JavaScript code requires an understanding of the scope of variables and methods.</p><p>IN SHORT: The scope of a function or variable refers to its visibility throughout the different parts of your code.</p><p><strong>VARIABLE SCOPE</strong></p><p>Variables in JavaScript can have either global or local scope. A global variable is created outside of a function and is accessible throughout the entire program. As opposed to this, a local variable is declared inside of a method and can only be accessed from within that function.</p><p>Consider the code below:</p><pre>var name = &quot;Bash&quot;;<br><br>function showName() {<br>  console.log(name);<br>}<br><br>showName();</pre><p>In this example, the variable “name” is declared outside of the “showName()” function, making it a global variable. The “showName()” function logs the value of name to the console. Since name is a global variable, it can be accessed inside the function without any issues.</p><p>However, if we declare “name” inside the function like this:</p><pre>function showName() {<br>  var name = &quot;Bash&quot;;<br>  console.log(name);<br>}</pre><p>In this case, “name” is a local variable because it is declared inside the function. This means that it can only be accessed within the function. If we try to access name outside of the function, we’ll get an error.</p><p>However, variables can cast shadows across ranges. Even if you use const, you can redefine one with the same name in a separate scope without getting an error. (Code below)</p><pre>const name = &#39;Bash&#39;<br><br>function showName() {<br>  const name = &#39;Bashir&#39;<br>  console.log(name)<br>}<br><br>console.log(name) // Bash<br>showName() // Bashir</pre><p><strong>FUNCTION SCOPE</strong></p><p>JavaScript functions also have their scope. Any variables declared inside a function are thus only usable inside that function. Consider the following example:</p><pre>function fullName(fName, lName) {<br>  var result = `${fName} ${lName}`;<br>  return result;<br>}<br><br><br>function showName() {<br>  const name = &#39;Bashir&#39;<br>}<br><br>console.log(name) // ReferenceError: name is not defined</pre><p>In this example, the “fullName()” function has two parameters, “fName” and “lName”, and a local variable called “result”. The “result” variable is only accessible within the function, so it can’t be accessed from outside the function.</p><p><strong>BLOCK SCOPE</strong></p><p>JavaScript features block scope in addition to global and local scope. A block is a chunk of code, such as a loop or conditional expression, that is surrounded by curly braces. All-access to variables declared inside a block is restricted to that block. Consider the following example:</p><pre>function printNumbers() {<br>  for (var i = 0; i &lt; 10; i++) {<br>    console.log(i);<br>  }<br>  console.log(i);<br>}<br><br><br>if (true) {<br>  const name = &#39;Bashir&#39;<br>}<br><br>console.log(name) // ReferenceError: name is not defined</pre><p>In this example, “the printNumbers()” function has a for loop that counts from 0 to 9 and logs each number to the console. After the loop, we’re trying to log the value of i again. However, since i was declared inside the for loop, it is only accessible within the loop. Trying to access it outside of the loop will result in an error. Second example, it won’t be available if the conditional statement doesn’t run.</p><p><strong>Scope </strong>is more complicated than just where the variable definition is placed. Depending on the word you choose to characterize it with. Whereas variables defined with let and const are block-scoped, those defined with var are function-scoped.</p><p>If a variable is declared using var and it is in the same function, it is accessible even outside of a block.</p><p><strong>CONCLUTION</strong></p><p>JavaScript variable and function scope can be a complex topic, but it’s essential for writing efficient and maintainable code. By understanding the different types of scope and how they work, you can avoid errors and write code that is easier to read and debug. Remember that variables declared outside of a function are global variables, variables declared inside a function are local variables, and variables declared inside a block are block-scoped variables. Functions also have their own scope, and any variables declared inside a function are only accessible within that function.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=94d122d1bbab" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>