Add global variable in our Projects (Javascript)

Anush Kumar N
2 min readJan 8, 2021
A simple Js image

I have seen some fancy variables in various web application where when we go to developer console and we can find their own global variable.

I got surprised and I was eagerly waiting to find how to do it. At last I found a simple way to do it.

Now let’s see how to do it, You need prior knowledge about Javascript global variable and Object.assign()to know the syntax and working behind it.

Now just to make you understand, let’s assume that we need a variable named Superman. So this will be the code to add that Superman as a global variable.

let Superman = {
name: 'Kal-El',
fatherName: 'Jor-El',
motherName: 'Lara',
origin: 'Kryptonian',
}
window.Superman = Object.assign({}, window.Superman, Superman);
// the above line will merge window.Superman & Superman objects

(Make sure you either run this code in developer console or in a JS file and then try this.) Now when you go to developer console and type Superman you will see the magic.

Yay!!

Note: When playing with global variables please know that you are going to load the window object with your values and please be very careful while doing this because it might affect the performance and do know the consequences too.

If you find this useful show it with claps it and share with others, I have also blogged other various useful things in medium please do check it out, thanks 😄

--

--