{"id":1612,"date":"2024-01-31T04:37:25","date_gmt":"2024-01-31T04:37:25","guid":{"rendered":"https:\/\/www.devopsfreelancer.com\/blog\/?p=1612"},"modified":"2024-02-06T05:00:14","modified_gmt":"2024-02-06T05:00:14","slug":"how-to-use-global-variable-using-javascript-in-laravel","status":"publish","type":"post","link":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-use-global-variable-using-javascript-in-laravel\/","title":{"rendered":"How to use Global Variable using JavaScript in Laravel ?"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">What is Global Variable ?<\/h2>\n\n\n\n<p>A global variable is a variable that is defined and accessible throughout the entire program or a significant portion of it, rather than being limited to a specific function or block of code. Global variables are declared outside of any function, typically at the top of a program, making them accessible from any part of the code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Benefits of using Global variable<\/h2>\n\n\n\n<p>While the use of global variables is generally discouraged due to potential issues like unintended side effects and decreased code maintainability, there are specific scenarios where global variables can be beneficial:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Global State Management:<\/strong> Global variables can be useful for managing global state within a program. For example, in some cases, a program might need to maintain a global configuration or settings that should be accessible from various parts of the code.<\/li>\n\n\n\n<li><strong>Constants:<\/strong> Global variables can be used to define constants that are accessed from multiple functions or modules. This can help centralize the definition of constants, making it easier to update them consistently throughout the code.<\/li>\n\n\n\n<li><strong>Simplifying Function Signatures:<\/strong> In some cases, using global variables can simplify function signatures by reducing the number of parameters that need to be passed. This can make the code cleaner and more readable, especially in situations where many functions need access to the same data.<\/li>\n<\/ol>\n\n\n\n<p>It&#8217;s important to note that while these benefits exist, caution should be exercised when using global variables to ensure that they are used judiciously and do not compromise the principles of good software design, such as modularity and encapsulation. In many cases, alternative approaches, such as passing parameters or using dependency injection, are preferred to minimize the potential downsides associated with global variables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to use it ?<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>var invitedByValues = &#91;];\n$.ajax({\n    type: 'get',\n    url: \"{{ url('api\/v1\/j\/invitedManagerUserGetting') }}\/\" + userAuthId + '\/' +\n        organisation_id,\n    dataType: \"json\",\n    async: false,\n    dataSrc: 'data',\n    \"beforeSend\": function(xhr) {\n        xhr.setRequestHeader(\"Authorization\", \"Bearer \" + localStorage.getItem(\n        'a_u_a_b_t'));\n    },\n    success: function(data) {\n        console.log('success main manager id value a rha hai ki nahi', data);\n        var projectManagerSelect = $('#project_manager');\n        \/\/ Do not re-declare invitedByValues, use the global one\n        invitedByValues = &#91;...new Set(data.map(item =&gt; item.invited_by))];\n        console.log('Unique invited_by values:', invitedByValues);\n        \/\/ Now you can use invitedByValues as needed\n        \/\/ For example, log each unique invited_by value\n        invitedByValues.forEach(invitedBy =&gt; {\n            console.log('Unique invited_by value:', invitedBy);\n        });\n        var authEmail = \"{{ Auth::user()-&gt;email }}\";\n        if (!projectManagerSelect.find('option&#91;value=\"' + authEmail + '\"]').length) {\n            projectManagerSelect.append('&lt;option value=\"' + authEmail + '\"&gt;' + authEmail +\n                '&lt;\/option&gt;');\n        }\n    }\n});\n<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Global Variable ? A global variable is a variable that is defined and accessible throughout the entire program or a significant portion of it, rather&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[199],"tags":[644,22,3,645],"class_list":["post-1612","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-global-variable","tag-javascript","tag-laravel","tag-use-global-variable"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to use Global Variable using JavaScript in Laravel ? - DevOps Freelancer<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.devopsfreelancer.com\/blog\/how-to-use-global-variable-using-javascript-in-laravel\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to use Global Variable using JavaScript in Laravel ? - DevOps Freelancer\" \/>\n<meta property=\"og:description\" content=\"What is Global Variable ? A global variable is a variable that is defined and accessible throughout the entire program or a significant portion of it, rather...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.devopsfreelancer.com\/blog\/how-to-use-global-variable-using-javascript-in-laravel\/\" \/>\n<meta property=\"og:site_name\" content=\"DevOps Freelancer\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/amitsthakurs\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-31T04:37:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-06T05:00:14+00:00\" \/>\n<meta name=\"author\" content=\"Amit Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/amits_thakurs\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Amit Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-use-global-variable-using-javascript-in-laravel\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-use-global-variable-using-javascript-in-laravel\\\/\"},\"author\":{\"name\":\"Amit Kumar\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#\\\/schema\\\/person\\\/22ed4bd82dc04200a2ca541b3e35fc5b\"},\"headline\":\"How to use Global Variable using JavaScript in Laravel ?\",\"datePublished\":\"2024-01-31T04:37:25+00:00\",\"dateModified\":\"2024-02-06T05:00:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-use-global-variable-using-javascript-in-laravel\\\/\"},\"wordCount\":295,\"commentCount\":0,\"keywords\":[\"global-variable\",\"javascript\",\"laravel\",\"use-global-variable\"],\"articleSection\":[\"Javascript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-use-global-variable-using-javascript-in-laravel\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-use-global-variable-using-javascript-in-laravel\\\/\",\"url\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-use-global-variable-using-javascript-in-laravel\\\/\",\"name\":\"How to use Global Variable using JavaScript in Laravel ? - DevOps Freelancer\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#website\"},\"datePublished\":\"2024-01-31T04:37:25+00:00\",\"dateModified\":\"2024-02-06T05:00:14+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#\\\/schema\\\/person\\\/22ed4bd82dc04200a2ca541b3e35fc5b\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-use-global-variable-using-javascript-in-laravel\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-use-global-variable-using-javascript-in-laravel\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-use-global-variable-using-javascript-in-laravel\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to use Global Variable using JavaScript in Laravel ?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/\",\"name\":\"DevOps Freelancer\",\"description\":\"We provide DevOps | SRE | DevSecOps | MLOps Freelancing\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#\\\/schema\\\/person\\\/22ed4bd82dc04200a2ca541b3e35fc5b\",\"name\":\"Amit Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g\",\"caption\":\"Amit Kumar\"},\"description\":\"Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer\\\/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/amitsthakurs\\\/\",\"https:\\\/\\\/www.instagram.com\\\/amits_thakurs\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/amits-thakurs\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/amits_thakurs\"],\"url\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/author\\\/amit\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to use Global Variable using JavaScript in Laravel ? - DevOps Freelancer","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:\/\/www.devopsfreelancer.com\/blog\/how-to-use-global-variable-using-javascript-in-laravel\/","og_locale":"en_US","og_type":"article","og_title":"How to use Global Variable using JavaScript in Laravel ? - DevOps Freelancer","og_description":"What is Global Variable ? A global variable is a variable that is defined and accessible throughout the entire program or a significant portion of it, rather...","og_url":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-use-global-variable-using-javascript-in-laravel\/","og_site_name":"DevOps Freelancer","article_author":"https:\/\/www.facebook.com\/amitsthakurs\/","article_published_time":"2024-01-31T04:37:25+00:00","article_modified_time":"2024-02-06T05:00:14+00:00","author":"Amit Kumar","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/amits_thakurs","twitter_misc":{"Written by":"Amit Kumar","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-use-global-variable-using-javascript-in-laravel\/#article","isPartOf":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-use-global-variable-using-javascript-in-laravel\/"},"author":{"name":"Amit Kumar","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b"},"headline":"How to use Global Variable using JavaScript in Laravel ?","datePublished":"2024-01-31T04:37:25+00:00","dateModified":"2024-02-06T05:00:14+00:00","mainEntityOfPage":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-use-global-variable-using-javascript-in-laravel\/"},"wordCount":295,"commentCount":0,"keywords":["global-variable","javascript","laravel","use-global-variable"],"articleSection":["Javascript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.devopsfreelancer.com\/blog\/how-to-use-global-variable-using-javascript-in-laravel\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-use-global-variable-using-javascript-in-laravel\/","url":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-use-global-variable-using-javascript-in-laravel\/","name":"How to use Global Variable using JavaScript in Laravel ? - DevOps Freelancer","isPartOf":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/#website"},"datePublished":"2024-01-31T04:37:25+00:00","dateModified":"2024-02-06T05:00:14+00:00","author":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b"},"breadcrumb":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-use-global-variable-using-javascript-in-laravel\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.devopsfreelancer.com\/blog\/how-to-use-global-variable-using-javascript-in-laravel\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-use-global-variable-using-javascript-in-laravel\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.devopsfreelancer.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to use Global Variable using JavaScript in Laravel ?"}]},{"@type":"WebSite","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#website","url":"https:\/\/www.devopsfreelancer.com\/blog\/","name":"DevOps Freelancer","description":"We provide DevOps | SRE | DevSecOps | MLOps Freelancing","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.devopsfreelancer.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b","name":"Amit Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g","caption":"Amit Kumar"},"description":"Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer\/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.","sameAs":["https:\/\/www.facebook.com\/amitsthakurs\/","https:\/\/www.instagram.com\/amits_thakurs\/","https:\/\/www.linkedin.com\/in\/amits-thakurs\/","https:\/\/x.com\/https:\/\/twitter.com\/amits_thakurs"],"url":"https:\/\/www.devopsfreelancer.com\/blog\/author\/amit\/"}]}},"_links":{"self":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1612","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/comments?post=1612"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1612\/revisions"}],"predecessor-version":[{"id":1618,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1612\/revisions\/1618"}],"wp:attachment":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/media?parent=1612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/categories?post=1612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/tags?post=1612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}