this post was submitted on 29 Dec 2023
224 points (79.8% liked)

Programmer Humor

32361 readers
124 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 

cross-posted from: https://lemmy.world/post/10094818

spoilerGender variability as declarations in JavaScript: const / let / var

Meme is based on Jordan Peterson "approival / disapproval" format, him being a conservative who disapproves of gender fluidity.

Transcript:

  • Jordan Peterson approval image: const gender;
  • Jordan Peterson angry image: let gender;
  • Jordan Peterson crying image: var gender;

you are viewing a single comment's thread
view the rest of the comments
[–] andrew@lemmy.stuart.fun 111 points 10 months ago (2 children)

Joke's on you because they're all still mutable objects behind the reference.

[–] Phen@lemmy.eco.br 28 points 10 months ago (2 children)

Last one can be freely changed by anyone, the middle one still has some restraints.

[–] soloner@lemmy.world 22 points 10 months ago (1 children)

Reassignment isn't the same as mutation. But mutation depends on the type of value. If gender was a string like "female" it wouldn't be mutable cuz strings are immutable in JS.

[–] andrew@lemmy.stuart.fun 5 points 10 months ago

Yeah this is true. My joke makes an assumption about the type not being a primitive type.

[–] andrew@lemmy.stuart.fun 7 points 10 months ago (1 children)

var isn't global unless it's not inside a function. var is just function scoped, with declaration auto hoisted to the beginning of the function. let is a little more intuitive since you can't refer to it before it's been declared and has block scope rather than function scope.

[–] Klaymore@sh.itjust.works 6 points 10 months ago (3 children)

Wait...... you can use a variable before you declare it?

[–] shotgun_crab@lemmy.world 6 points 10 months ago

Classic javascript doing javascript things (this is why they introduced let and const)

[–] CheezyWeezle@lemmy.world 3 points 10 months ago* (last edited 10 months ago)

Kind of. With hoisting, the compiler/interpreter will find variable declarations and execute them before executing the rest of the code. Hoisting leaves the variables as undefined until the code assigning the value to the variable is executed. Hoisting does not initialize the variables.

For example:

console.log(foo);
var foo;
//Expected output: console logs 'null'

foo = 'bar';
console.log(foo);
var foo;
//Expected output: console logs 'bar'

console.log(foo === undefined);
var foo;
//Expected output: console logs 'true'

This means you can essentially write your code with variable declarations at the end, but it will still be executed as though the declarations were at the beginning. Your initializations and value assignments will still be executed as normal.

This is a feature that you should probably avoid because I honestly cannot think of any good use case for it that won't end up causing confusion, but it is important to understand that every variable within your scope will be declared at the beginning of execution regardless of where it is written within your code.

[–] andrew@lemmy.stuart.fun 2 points 10 months ago* (last edited 10 months ago)
var a;
(function() {
  a='hoisted';
  console.log(a);
  var a;
})()
console.log(a);

Should log hoisted and then undefined, showing that you've assigned to the later-declared var a which was hoisted vs the external global a.

[–] lseif@sopuli.xyz 4 points 10 months ago (1 children)

typescript: const as const (readonly) 'pretty please dont mutate it'

[–] Baizey@feddit.dk 3 points 10 months ago