JavaScript

12 Ways to Create Variables in JavaScript

In this video, I’ll show you how to create variables in JavaScript using 12 different ways. Chances are you will probably never use most of them in your code, but for the love of coding and because JavaScript lets you do it, we’ll explore its capabilities.

#1. Using let
#2. Using const
#3. Using var
#4. Using no keyword (globally)
#5. Using globalThis keyword
#6. Using this keyword
#7. Using window keyword
#8. Using window’s constructor’s prototype property
#9. Using this constructor’s prototype property
#10. Using this constructor’s prototype property
#11. Using object instance __proto__
#12. Using object literal __proto__

const data = ["-- offset index keys --"]
/*-------------------------------*/

//#1:  let
let a = "Aiden";

//#2: const
const b = "Bailey";

//#3: var
var c = "Charlie";

//#4: <global>
d = "Dawn";

//#5: globalThis
globalThis.e = "Eli";

//#6: this
this.f = "Fiona";

//#7: window
window.g = "Gaius";

//#8: window's constructor's prototype
window.constructor.prototype.h = "Helen";

//#9: this as in #8
this.constructor.prototype.i = "Ichiro";

//#10: Object's prototype
Object.prototype.j = "Jasmine";

//#11: Object instance __proto__
//const obj = new Object();
const obj = {};
obj.__proto__.k = "Kal-el";

//#12: Object literal __proto__
//(new Object()).__proto__.l = "Lana";
({}).__proto__.l = "Lana";

/*-------------------------------*/
data.push(a,b,c,d,e,f,g,h,i,j,k,l)
console.table(data);
Verified by MonsterInsights