How to use Global Variable using JavaScript in Laravel ?

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 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.

Benefits of using Global variable

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:

  1. Global State Management: 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.
  2. Constants: 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.
  3. Simplifying Function Signatures: 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.

It’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.

How to use it ?

var invitedByValues = [];
    type: 'get',
    url: "{{ url('api/v1/j/invitedManagerUserGetting') }}/" + userAuthId + '/' +
    dataType: "json",
    async: false,
    dataSrc: 'data',
    "beforeSend": function(xhr) {
        xhr.setRequestHeader("Authorization", "Bearer " + localStorage.getItem(
    success: function(data) {
        console.log('success main manager id value a rha hai ki nahi', data);
        var projectManagerSelect = $('#project_manager');
        // Do not re-declare invitedByValues, use the global one
        invitedByValues = [ Set( => item.invited_by))];
        console.log('Unique invited_by values:', invitedByValues);
        // Now you can use invitedByValues as needed
        // For example, log each unique invited_by value
        invitedByValues.forEach(invitedBy => {
            console.log('Unique invited_by value:', invitedBy);
        var authEmail = "{{ Auth::user()->email }}";
        if (!projectManagerSelect.find('option[value="' + authEmail + '"]').length) {
            projectManagerSelect.append('<option value="' + authEmail + '">' + authEmail +

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.

Related Posts

How to print Object Value using Array in Javascript ?

In this tutorial i’m going to print the object value using javascript ? First go to controller function and put below code. Next go to blade page…

How to Count the Vowels value in array objects ?

Ini this tutorial we’re going to learn how to get the value form array object and count only vowels value. 1st step create below file name Put…

How to print multidimensional array in Console ?

In this tutorial we’re going to learn how to print multidimensional array in console.log Using this I want to print all data whatever coming in log file…

How to remove fake path from url in laravel ?

In this tutorial we’re going to learn how to remove fake path from url in laravel using javascript. Now fakepath removing successfully form url 👍

List of My Blogs Written from May to October 2023

List of My Blogs Written in May 2023 1 How to Show Notification using Sweet Alert in Laravel 9 2 How to use Soft Delete and Restore…

How to Convert Array to Object in Javascript?

What is Array Object ? JavaScript, arrays are a special type of object used for storing and manipulating lists of values. Despite their name, arrays in JavaScript…

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x