{"id":552,"date":"2025-05-19T11:02:26","date_gmt":"2025-05-19T11:02:26","guid":{"rendered":"https:\/\/buhave.com\/courses\/?p=552"},"modified":"2026-06-06T12:44:02","modified_gmt":"2026-06-06T12:44:02","slug":"functions-and-scope","status":"publish","type":"post","link":"https:\/\/buhave.com\/courses\/java-script\/functions-and-scope\/","title":{"rendered":"Functions and Scope"},"content":{"rendered":"<h2>Function Declaration and Expression<\/h2>\n<h3>Function Declaration<\/h3>\n<p>A Function Declaration is the classic way to define a named function that participates directly in your program&#8217;s main flow. For a broader look at function concepts, see our <a href=\"https:\/\/buhave.com\/courses\/python\/functions\/\" title=\"Functions module\">Functions module<\/a>.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>function functionName(parameters) {<\/em><br \/>\n<em>\/\/ code block<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong> Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>function add(a, b) {<\/em><br \/>\n<em>return a + b;<\/em><br \/>\n<em>}<\/em><\/p>\n<p style=\"text-align: center\"><em>console.log(add(2, 3)); \/\/ Output: 5<\/em><\/p>\n<p><strong>Key Features:<\/strong><\/p>\n<ul>\n<li>Named function (like add above).<\/li>\n<li>Hoisted: Can be called before it appears in the code.<\/li>\n<li>Good for defining main functions used throughout the program.<\/li>\n<\/ul>\n<h3>Function Expression<\/h3>\n<p>A Function Expression is a function assigned to a variable, which may be anonymous or named. It can be anonymous or named. For broader context, see the Introduction to JavaScript guide.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>const functionName = function(parameters) {<\/em><br \/>\n<em>\/\/ code block<\/em><br \/>\n<em>};<\/em><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>const multiply = function(a, b) {<\/em><br \/>\n<em>return a * b;<\/em><br \/>\n<em>};<\/em><\/p>\n<p style=\"text-align: center\"><em>console.log(multiply(4, 5)); \/\/ Output: 20<\/em><\/p>\n<p><strong>Key Features:<\/strong><\/p>\n<ul>\n<li>Can be anonymous (no name) or named (rarely).<\/li>\n<li>Not hoisted: You must define it before you call it.<\/li>\n<li>Useful for passing functions as arguments or storing them in variables.<\/li>\n<\/ul>\n<p><strong>Key Differences Between Declaration and Expression:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>Feature<\/th>\n<th>Function Declaration<\/th>\n<th>Function Expression<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Name<\/td>\n<td>Always named<\/td>\n<td>Can be anonymous or named<\/td>\n<\/tr>\n<tr>\n<td>Hoisting<\/td>\n<td class=\"yes\">Yes<\/td>\n<td class=\"no\">No<\/td>\n<\/tr>\n<tr>\n<td>When defined<\/td>\n<td>During the code parsing phase<\/td>\n<td>During the execution phase<\/td>\n<\/tr>\n<tr>\n<td>Common Use<\/td>\n<td>Defining main reusable blocks<\/td>\n<td>Assigning to variables, callbacks<\/td>\n<\/tr>\n<tr>\n<td>Example usage<\/td>\n<td><code>function add(a, b) {}<\/code><\/td>\n<td><code>const add = function(a, b) {}<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Hoisting Example:<\/strong><\/p>\n<p><strong>Function Declaration (Works before defining):<\/strong><\/p>\n<p style=\"text-align: center\"><em>sayHello(); \/\/ Output: Hello<\/em><\/p>\n<p style=\"text-align: center\"><em>function sayHello() {<\/em><br \/>\n<em>console.log(&#8216;Hello&#8217;);<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>Function Expression (Fails if called early):<\/strong><\/p>\n<p style=\"text-align: center\"><em>sayHi(); \/\/ Error: Cannot access &#8216;sayHi&#8217; before initialization<\/em><\/p>\n<p style=\"text-align: center\"><em>const sayHi = function() {<\/em><br \/>\n<em>console.log(&#8216;Hi&#8217;);<\/em><br \/>\n<em>};<\/em><\/p>\n<p><strong>Best Practices:<\/strong><\/p>\n<ul>\n<li>Use function declarations for functions you call throughout the code.<\/li>\n<li>Use function expressions for callbacks, dynamic functions, or inside event listeners.<\/li>\n<li>In modern JavaScript, prefer arrow functions (()=&gt;{}) for short expressions.<\/li>\n<\/ul>\n<h2>Parameters, Arguments, Return values<\/h2>\n<h3>1. Parameters<\/h3>\n<p>Parameters are variables listed inside the parentheses of a function definition.<br \/>\nThey act as placeholders for the values (arguments) that the function will receive when it is called.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>function greet(name) {<\/em><br \/>\n<em>console.log(`Hello, ${name}`);<\/em><br \/>\n<em>}<\/em><\/p>\n<ul>\n<li>Here, name is the parameter.<\/li>\n<li>It is a local variable inside the function.<\/li>\n<\/ul>\n<h3>2. Arguments<\/h3>\n<p>Arguments are the actual values you pass into a function when you call it.<br \/>\nThey are assigned to the function\u2019s parameters.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>greet(&#8220;Alice&#8221;);<\/em><\/p>\n<ul>\n<li>&#8220;Alice&#8221; is the argument passed to the greet function.<\/li>\n<li>The function uses &#8220;Alice&#8221; wherever name is referenced.<\/li>\n<\/ul>\n<p><strong>Important Points:<\/strong><\/p>\n<ul>\n<li>The number of parameters and arguments should match, but JavaScript is flexible:<\/li>\n<li>Extra arguments are ignored.<\/li>\n<li>Missing arguments become undefined.<\/li>\n<\/ul>\n<p style=\"text-align: center\"><em>function displayInfo(name, age) {<\/em><br \/>\n<em>console.log(`${name} is ${age} years old.`);<\/em><br \/>\n<em>}<\/em><\/p>\n<p style=\"text-align: center\"><em>displayInfo(&#8220;Bob&#8221;); <\/em><br \/>\n<em>\/\/ Output: Bob is undefined years old.<\/em><\/p>\n<h3>3. Return Values<\/h3>\n<ul>\n<li>Return values are the results that a function sends back after it finishes running.<\/li>\n<li>You use the return keyword to send data out of the function.<\/li>\n<\/ul>\n<p><strong>Syntax:<\/strong><\/p>\n<p style=\"text-align: center\"><em>function add(a, b) {<\/em><br \/>\n<em>return a + b;<\/em><br \/>\n<em>}<\/em><\/p>\n<p style=\"text-align: center\"><em>const result = add(2, 3);<\/em><br \/>\n<em>console.log(result); \/\/ Output: 5<\/em><\/p>\n<ul>\n<li>return a + b; sends back the sum.<\/li>\n<li>The result variable now holds the value 5.<\/li>\n<\/ul>\n<p><strong>Important Rules About return:<\/strong><\/p>\n<ul>\n<li>A function stops executing immediately after return.<\/li>\n<li>If no return is specified, the function returns undefined by default.<\/li>\n<li>You can return any type: numbers, strings, objects, arrays, even another function.<\/li>\n<\/ul>\n<p style=\"text-align: center\"><em>function noReturn() {<\/em><br \/>\n<em>console.log(&#8220;This function doesn&#8217;t return anything explicitly.&#8221;);<\/em><br \/>\n<em>}<\/em><\/p>\n<p style=\"text-align: center\"><em>console.log(noReturn()); <\/em><br \/>\n<em>\/\/ Output: This function doesn&#8217;t return anything explicitly.<\/em><br \/>\n<em>\/\/ undefined<\/em><\/p>\n<p><strong>Quick Summary Table:<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>Term<\/th>\n<th>Meaning<\/th>\n<th>Example<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Parameter<\/td>\n<td>Placeholder variable in function definition<\/td>\n<td><code>function greet(name)<\/code><\/td>\n<\/tr>\n<tr>\n<td>Argument<\/td>\n<td>Actual value passed into the function<\/td>\n<td><code>greet(\"Alice\")<\/code><\/td>\n<\/tr>\n<tr>\n<td>Return Value<\/td>\n<td>The output a function sends back<\/td>\n<td><code>return a + b<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Best Practices:<\/strong><\/p>\n<ul>\n<li>Always name parameters clearly for readability.<\/li>\n<li>Ensure correct number of arguments are passed.<\/li>\n<li>Use return to make functions more reusable and predictable.<\/li>\n<\/ul>\n<h2>Arrow functions<\/h2>\n<h3>JavaScript Arrow Functions (ES6+)<\/h3>\n<p>Arrow functions are a concise syntax for defining functions, introduced in ES6 (ECMAScript 2015). They reduce boilerplate, provide lexical this (they do not bind their own this), and are broadly supported by modern browsers and Node.js.<\/p>\n<p><strong>1. Basic Syntax of Arrow Functions<\/strong><\/p>\n<p><strong>Traditional Function:<\/strong><\/p>\n<p style=\"text-align: center\"><em>function add(a, b) {<\/em><br \/>\n<em>return a + b;<\/em><br \/>\n<em>}<\/em><\/p>\n<p><strong>Arrow Function (equivalent):<\/strong><\/p>\n<p style=\"text-align: center\"><em>const add = (a, b) =&gt; {<\/em><br \/>\n<em>return a + b;<\/em><br \/>\n<em>};<\/em><\/p>\n<ul>\n<li>function keyword is removed.<\/li>\n<li>Arrow (=&gt;) is used instead.<\/li>\n<li>The function is usually assigned to a variable.<\/li>\n<\/ul>\n<p><strong>2. Shortened Syntax for Simple Returns<\/strong><\/p>\n<p>If the function has only one expression, you can omit {} and return completely.<\/p>\n<p style=\"text-align: center\"><em>const add = (a, b) =&gt; a + b;<\/em><\/p>\n<ul>\n<li>Clean and automatic return of the value.<\/li>\n<\/ul>\n<p><strong>3. Arrow Function Variations<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>Case<\/th>\n<th>Syntax Example<\/th>\n<th>Notes<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Multiple Parameters<\/td>\n<td><code>(a, b) =&gt; a + b<\/code><\/td>\n<td class=\"notes\">Use parentheses for two or more params<\/td>\n<\/tr>\n<tr>\n<td>Single Parameter<\/td>\n<td><code>name =&gt; console.log(name)<\/code><\/td>\n<td class=\"notes\">Parentheses optional if one param<\/td>\n<\/tr>\n<tr>\n<td>No Parameters<\/td>\n<td><code>() =&gt; console.log('Hello')<\/code><\/td>\n<td class=\"notes\">Always need parentheses<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Examples:<\/strong><\/p>\n<p><strong>One parameter:<\/strong><\/p>\n<p style=\"text-align: center\"><em>const square = x =&gt; x * x;<\/em><br \/>\n<em>console.log(square(5)); \/\/ Output: 25<\/em><\/p>\n<p><strong>No parameter:<\/strong><\/p>\n<p style=\"text-align: center\"><em>const greet = () =&gt; console.log(&#8220;Hello!&#8221;);<\/em><br \/>\n<em>greet(); \/\/ Output: Hello!<\/em><\/p>\n<p><strong>More complex body (need {} and return):<\/strong><\/p>\n<p style=\"text-align: center\"><em>const sum = (a, b) =&gt; {<\/em><br \/>\n<em>const result = a + b;<\/em><br \/>\n<em>return result;<\/em><br \/>\n<em>};<\/em><br \/>\n<em>console.log(sum(2, 3)); \/\/ Output: 5<\/em><\/p>\n<p><strong>4. Special Behavior of this in Arrow Functions<\/strong><\/p>\n<ul>\n<li>Arrow functions do NOT have their own this context.<\/li>\n<li>They inherit this from the surrounding (parent) scope.<\/li>\n<\/ul>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>function Person() {<\/em><br \/>\n<em>this.name = &#8220;Alice&#8221;;<\/em><\/p>\n<p><em>setTimeout(() =&gt; {<\/em><br \/>\n<em>console.log(`Hello, ${this.name}`);<\/em><br \/>\n<em>}, 1000);<\/em><br \/>\n<em>}<\/em><\/p>\n<p style=\"text-align: center\"><em>const p = new Person();<\/em><br \/>\n<em>\/\/ Output after 1 second: Hello, Alice<\/em><\/p>\n<ul>\n<li>If you used a regular function inside setTimeout, this.name would be undefined.<\/li>\n<li>Arrow function keeps the this of Person.<\/li>\n<\/ul>\n<p><strong>Arrow Function vs Regular Function (Key Differences)<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>Feature<\/th>\n<th>Regular Function<\/th>\n<th>Arrow Function<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td class=\"highlight\">Syntax<\/td>\n<td class=\"regular\">Longer (function keyword needed)<\/td>\n<td class=\"arrow\">Shorter and cleaner (=&gt;)<\/td>\n<\/tr>\n<tr>\n<td class=\"highlight\">this Behavior<\/td>\n<td class=\"regular\">Dynamic based on how it&#8217;s called<\/td>\n<td class=\"arrow\">Lexical (inherits from parent)<\/td>\n<\/tr>\n<tr>\n<td class=\"highlight\">Suitable for<\/td>\n<td class=\"regular\">Constructors, methods, complex logic<\/td>\n<td class=\"arrow\">Short functions, callbacks<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>When to Use Arrow Functions<\/strong><\/p>\n<ul>\n<li>When you want shorter syntax for simple operations.<\/li>\n<li>When you need to preserve the surrounding this.<\/li>\n<li>Ideal for array methods like map(), filter(), reduce().<\/li>\n<li>Good for event handlers, callbacks, promises, and simple utility functions.<\/li>\n<\/ul>\n<p><strong>Quick Summary:<\/strong><\/p>\n<ul>\n<li>Arrow functions are short, clean, and lexical-scoped versions of functions.<\/li>\n<li>No separate this, arguments, or super.<\/li>\n<li>Not ideal for methods inside objects or for constructor functions.<\/li>\n<\/ul>\n<p>Arrow functions are a concise syntax for defining functions and are broadly supported in modern environments since their introduction in ES6 (2015).<\/p>\n<p>For practical examples, see Array methods in our <a href=\"https:\/\/buhave.com\/courses\/java-script\/arrays-and-objects\/\" title=\"Arrays and Objects lesson\">Arrays and Objects<\/a> lesson.<\/p>\n<h2>Variable scope (global, local, block)<\/h2>\n<p><strong>What is Scope?<\/strong><\/p>\n<p>Scope refers to where a variable is accessible in your code.<br \/>\nIt defines the visibility and lifetime of variables.<\/p>\n<h3>1. Global Scope<\/h3>\n<ul>\n<li>A variable has global scope if it is declared outside any function or block.<\/li>\n<li>It can be accessed anywhere in the program.<\/li>\n<\/ul>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>let globalVar = &#8220;I am global&#8221;;<\/em><\/p>\n<p style=\"text-align: center\"><em>function showGlobal() {<\/em><br \/>\n<em>console.log(globalVar); \/\/ Can access globalVar<\/em><br \/>\n<em>}<\/em><\/p>\n<p style=\"text-align: center\"><em>showGlobal();<\/em><br \/>\n<em>console.log(globalVar); \/\/ Accessible here too<\/em><\/p>\n<ul>\n<li>Global variables are available throughout the entire script.<\/li>\n<\/ul>\n<p><strong>Caution:<\/strong><\/p>\n<ul>\n<li>Too many global variables can cause conflicts and bugs, especially in large programs.<\/li>\n<\/ul>\n<h3>2. Local Scope (Function Scope)<\/h3>\n<ul>\n<li>A variable has local scope if it is declared inside a function.<\/li>\n<li>It is accessible only within that function.<\/li>\n<\/ul>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>function greet() {<\/em><br \/>\n<em>let message = &#8220;Hello, World!&#8221;;<\/em><br \/>\n<em>console.log(message); \/\/ Accessible here<\/em><br \/>\n<em>}<\/em><\/p>\n<p style=\"text-align: center\"><em>greet();<\/em><br \/>\n<em>console.log(message); \/\/ Error: message is not defined<\/em><\/p>\n<ul>\n<li>Local variables cannot be accessed outside the function.<\/li>\n<\/ul>\n<h3>3. Block Scope<\/h3>\n<ul>\n<li>Variables declared with let and const are block-scoped.<\/li>\n<li>A block is anything between {} \u2014 like loops, conditionals, or simply {}.<\/li>\n<\/ul>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"text-align: center\"><em>{<\/em><br \/>\n<em>let blockVar = &#8220;Inside block&#8221;;<\/em><br \/>\n<em>console.log(blockVar); \/\/ Accessible inside block<\/em><br \/>\n<em>}<\/em><\/p>\n<p style=\"text-align: center\"><em>console.log(blockVar); \/\/ Error: blockVar is not defined<\/em><\/p>\n<ul>\n<li>let and const create block-scoped variables.<\/li>\n<li>var does not respect block scope (only function or global).<\/li>\n<\/ul>\n<p><strong>Quick Comparison of var, let, and const Scope<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>Keyword<\/th>\n<th>Scope Type<\/th>\n<th>Hoisting<\/th>\n<th>Reassignable<\/th>\n<th>Redeclarable<\/th>\n<th>Example Use<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td class=\"keyword\">var<\/td>\n<td>Function or Global<\/td>\n<td class=\"yes\">Yes<\/td>\n<td class=\"yes\">Yes<\/td>\n<td class=\"yes\">Yes<\/td>\n<td class=\"example\">Old codebases<\/td>\n<\/tr>\n<tr>\n<td class=\"keyword\">let<\/td>\n<td>Block<\/td>\n<td class=\"yes\">Yes*<\/td>\n<td class=\"yes\">Yes<\/td>\n<td class=\"no\">No<\/td>\n<td class=\"example\">Loops, modern code<\/td>\n<\/tr>\n<tr>\n<td class=\"keyword\">const<\/td>\n<td>Block<\/td>\n<td class=\"yes\">Yes*<\/td>\n<td class=\"no\">No<\/td>\n<td class=\"no\">No<\/td>\n<td class=\"example\">Constants<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Function Declaration and Expression Function Declaration A Function Declaration is the classic way to define a named function that participates [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":757,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[],"class_list":["post-552","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-script"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Functions and Scope - Java Script Course<\/title>\n<meta name=\"description\" content=\"Functions in JavaScript create reusable, organized code blocks and define their own isolated local scope for variable accessibility.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/buhave.com\/courses\/java-script\/functions-and-scope\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Functions and Scope - Java Script Course\" \/>\n<meta property=\"og:description\" content=\"Functions in JavaScript create reusable, organized code blocks and define their own isolated local scope for variable accessibility.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/buhave.com\/courses\/java-script\/functions-and-scope\/\" \/>\n<meta property=\"og:site_name\" content=\"BUHAVE\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/BeYouHave\/\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/naveedsafdarawan\/\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-19T11:02:26+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-06-06T12:44:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/05\/Functions-and-Scope.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Naveed Safdar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Naveed Safdar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/functions-and-scope\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/functions-and-scope\\\/\"},\"author\":{\"name\":\"Naveed Safdar\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/person\\\/04fe0254e118521c9fbb3da39de5acca\"},\"headline\":\"Functions and Scope\",\"datePublished\":\"2025-05-19T11:02:26+00:00\",\"dateModified\":\"2026-06-06T12:44:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/functions-and-scope\\\/\"},\"wordCount\":1222,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/functions-and-scope\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/05\\\/Functions-and-Scope.webp\",\"articleSection\":[\"Java Script Course\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/functions-and-scope\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/functions-and-scope\\\/\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/functions-and-scope\\\/\",\"name\":\"Functions and Scope - Java Script Course\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/functions-and-scope\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/functions-and-scope\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/05\\\/Functions-and-Scope.webp\",\"datePublished\":\"2025-05-19T11:02:26+00:00\",\"dateModified\":\"2026-06-06T12:44:02+00:00\",\"description\":\"Functions in JavaScript create reusable, organized code blocks and define their own isolated local scope for variable accessibility.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/functions-and-scope\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/functions-and-scope\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/functions-and-scope\\\/#primaryimage\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/05\\\/Functions-and-Scope.webp\",\"contentUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/05\\\/Functions-and-Scope.webp\",\"width\":1200,\"height\":628,\"caption\":\"Functions and Scope\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/java-script\\\/functions-and-scope\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Courses\",\"item\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Script Course\",\"item\":\"https:\\\/\\\/buhave.com\\\/courses\\\/learn\\\/java-script\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Functions and Scope\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#website\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\",\"name\":\"BUHAVE\",\"description\":\"Courses - Learn Online for Free\",\"publisher\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/buhave.com\\\/courses\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#organization\",\"name\":\"BUHAVE\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/03\\\/buhave-course.webp\",\"contentUrl\":\"https:\\\/\\\/buhave.com\\\/courses\\\/wp-content\\\/uploads\\\/2025\\\/03\\\/buhave-course.webp\",\"width\":375,\"height\":75,\"caption\":\"BUHAVE\"},\"image\":{\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/BeYouHave\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/buhave\",\"https:\\\/\\\/www.youtube.com\\\/@buhave\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/buhave.com\\\/courses\\\/#\\\/schema\\\/person\\\/04fe0254e118521c9fbb3da39de5acca\",\"name\":\"Naveed Safdar\",\"description\":\"I\u2019m Naveed Safdar - SEO Manager with over 10 years of experience in SEO and Digital Marketing. I\u2019ve had the privilege of working with leading national and international companies including Grafdom, PakWheels, Systems Limited, Confiz, Educative, and Dubizzle Labs. My expertise spans technical SEO, content strategy, organic growth, and performance analytics - helping businesses improve visibility, traffic, and ROI.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/naveedsafdar\\\/\",\"https:\\\/\\\/www.facebook.com\\\/naveedsafdarawan\\\/\",\"https:\\\/\\\/www.youtube.com\\\/@naveedsafdar\"],\"url\":\"https:\\\/\\\/buhave.com\\\/courses\\\/author\\\/naveed-safdar\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Functions and Scope - Java Script Course","description":"Functions in JavaScript create reusable, organized code blocks and define their own isolated local scope for variable accessibility.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/buhave.com\/courses\/java-script\/functions-and-scope\/","og_locale":"en_US","og_type":"article","og_title":"Functions and Scope - Java Script Course","og_description":"Functions in JavaScript create reusable, organized code blocks and define their own isolated local scope for variable accessibility.","og_url":"https:\/\/buhave.com\/courses\/java-script\/functions-and-scope\/","og_site_name":"BUHAVE","article_publisher":"https:\/\/www.facebook.com\/BeYouHave\/","article_author":"https:\/\/www.facebook.com\/naveedsafdarawan\/","article_published_time":"2025-05-19T11:02:26+00:00","article_modified_time":"2026-06-06T12:44:02+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/05\/Functions-and-Scope.webp","type":"image\/webp"}],"author":"Naveed Safdar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Naveed Safdar","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/buhave.com\/courses\/java-script\/functions-and-scope\/#article","isPartOf":{"@id":"https:\/\/buhave.com\/courses\/java-script\/functions-and-scope\/"},"author":{"name":"Naveed Safdar","@id":"https:\/\/buhave.com\/courses\/#\/schema\/person\/04fe0254e118521c9fbb3da39de5acca"},"headline":"Functions and Scope","datePublished":"2025-05-19T11:02:26+00:00","dateModified":"2026-06-06T12:44:02+00:00","mainEntityOfPage":{"@id":"https:\/\/buhave.com\/courses\/java-script\/functions-and-scope\/"},"wordCount":1222,"commentCount":0,"publisher":{"@id":"https:\/\/buhave.com\/courses\/#organization"},"image":{"@id":"https:\/\/buhave.com\/courses\/java-script\/functions-and-scope\/#primaryimage"},"thumbnailUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/05\/Functions-and-Scope.webp","articleSection":["Java Script Course"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/buhave.com\/courses\/java-script\/functions-and-scope\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/buhave.com\/courses\/java-script\/functions-and-scope\/","url":"https:\/\/buhave.com\/courses\/java-script\/functions-and-scope\/","name":"Functions and Scope - Java Script Course","isPartOf":{"@id":"https:\/\/buhave.com\/courses\/#website"},"primaryImageOfPage":{"@id":"https:\/\/buhave.com\/courses\/java-script\/functions-and-scope\/#primaryimage"},"image":{"@id":"https:\/\/buhave.com\/courses\/java-script\/functions-and-scope\/#primaryimage"},"thumbnailUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/05\/Functions-and-Scope.webp","datePublished":"2025-05-19T11:02:26+00:00","dateModified":"2026-06-06T12:44:02+00:00","description":"Functions in JavaScript create reusable, organized code blocks and define their own isolated local scope for variable accessibility.","breadcrumb":{"@id":"https:\/\/buhave.com\/courses\/java-script\/functions-and-scope\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/buhave.com\/courses\/java-script\/functions-and-scope\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/buhave.com\/courses\/java-script\/functions-and-scope\/#primaryimage","url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/05\/Functions-and-Scope.webp","contentUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/05\/Functions-and-Scope.webp","width":1200,"height":628,"caption":"Functions and Scope"},{"@type":"BreadcrumbList","@id":"https:\/\/buhave.com\/courses\/java-script\/functions-and-scope\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Courses","item":"https:\/\/buhave.com\/courses\/"},{"@type":"ListItem","position":2,"name":"Java Script Course","item":"https:\/\/buhave.com\/courses\/learn\/java-script\/"},{"@type":"ListItem","position":3,"name":"Functions and Scope"}]},{"@type":"WebSite","@id":"https:\/\/buhave.com\/courses\/#website","url":"https:\/\/buhave.com\/courses\/","name":"BUHAVE","description":"Courses - Learn Online for Free","publisher":{"@id":"https:\/\/buhave.com\/courses\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/buhave.com\/courses\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/buhave.com\/courses\/#organization","name":"BUHAVE","url":"https:\/\/buhave.com\/courses\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/buhave.com\/courses\/#\/schema\/logo\/image\/","url":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/03\/buhave-course.webp","contentUrl":"https:\/\/buhave.com\/courses\/wp-content\/uploads\/2025\/03\/buhave-course.webp","width":375,"height":75,"caption":"BUHAVE"},"image":{"@id":"https:\/\/buhave.com\/courses\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/BeYouHave\/","https:\/\/www.linkedin.com\/company\/buhave","https:\/\/www.youtube.com\/@buhave"]},{"@type":"Person","@id":"https:\/\/buhave.com\/courses\/#\/schema\/person\/04fe0254e118521c9fbb3da39de5acca","name":"Naveed Safdar","description":"I\u2019m Naveed Safdar - SEO Manager with over 10 years of experience in SEO and Digital Marketing. I\u2019ve had the privilege of working with leading national and international companies including Grafdom, PakWheels, Systems Limited, Confiz, Educative, and Dubizzle Labs. My expertise spans technical SEO, content strategy, organic growth, and performance analytics - helping businesses improve visibility, traffic, and ROI.","sameAs":["https:\/\/www.linkedin.com\/in\/naveedsafdar\/","https:\/\/www.facebook.com\/naveedsafdarawan\/","https:\/\/www.youtube.com\/@naveedsafdar"],"url":"https:\/\/buhave.com\/courses\/author\/naveed-safdar\/"}]}},"_links":{"self":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/comments?post=552"}],"version-history":[{"count":4,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/552\/revisions"}],"predecessor-version":[{"id":1143,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/posts\/552\/revisions\/1143"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/media\/757"}],"wp:attachment":[{"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/media?parent=552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/categories?post=552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buhave.com\/courses\/wp-json\/wp\/v2\/tags?post=552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}