this post was submitted on 29 Dec 2023
205 points (78.7% liked)

Programmer Humor

31224 readers
562 users here now

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

Rules:

founded 4 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;

top 40 comments
sorted by: hot top controversial new old
[–] [email protected] 104 points 6 months ago (2 children)

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

[–] [email protected] 28 points 6 months ago (2 children)

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

[–] soloner 21 points 6 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.

[–] [email protected] 5 points 6 months ago

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

[–] [email protected] 7 points 6 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.

[–] [email protected] 6 points 6 months ago (3 children)

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

[–] shotgun_crab 6 points 6 months ago

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

[–] CheezyWeezle 3 points 6 months ago* (last edited 6 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.

[–] [email protected] 2 points 6 months ago* (last edited 6 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.

[–] [email protected] 4 points 6 months ago (1 children)

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

[–] [email protected] 3 points 6 months ago

// @ts-ignore

[–] [email protected] 49 points 6 months ago

Douchebag Peterson doesn't deserve to be a meme.

[–] [email protected] 26 points 6 months ago

void* gender it can be anything i want >:)

[–] [email protected] 19 points 6 months ago* (last edited 6 months ago) (1 children)

python:

gender: Optional[str] = None

[–] [email protected] 7 points 6 months ago (1 children)

How about:

gender: Optional[complex]
[–] [email protected] 7 points 6 months ago

self.gender = 1.63-2.1j 🤔

[–] mypasswordis1234 10 points 6 months ago (2 children)

Can anyone explain it to me, please? 🥹

[–] [email protected] 28 points 6 months ago (2 children)

In JavaScript, a const variable is an immutable constant that you cannot reassign. Similar to how many conservatives think of gender, an intrinsic fact of a person that you can only read, but never change.

The "let" keyword declares a variable in a local scope, the nearest surrounding curly braces. It can be changed in that scope, but does not exist anywhere else. I assume this is meant to concede that gender is a spectrum and your presentation can kind of wiggle, such as between "very manly" and "not as manly" but still a man. Like, a stereotypical lumberjack and a stereotypical twink are both men so there isn't "one way to be a man" but a conservative might say " but they are still men, you can change how you present but you can't change sex".

The "var" keyword lifts the variable definition to the top of the function, or "hoists" it up. A variable declared with var can be accessed and modified anywhere after the block it was declared in. Gender is a spectrum and it can be reassigned anywhere, at anytime, to anything.

[–] [email protected] 4 points 6 months ago

I interpret it a bit differently. After all, a variable declared with var isn't really more capable of being rebound, or bound to more values, than one declared with let. However, it is possible, with var, that setting a variable in one place could change it unexpectedly in another, so Rose Noble coming out as trans could cause Jordan Peterson to also suddenly be a woman.

[–] Tyfud 2 points 6 months ago (1 children)

Const keyword means constant, a value that won't change after the application has been compiled; this allows for certain optimizations.

Let keyword is a JavaScript variable that is safely scoped down to the method or function level.

Var keyword is generally discouraged in JavaScript, because it's a global declaration. The value of it could be available anywhere in the application, and the app might have collisions.

So, the meme is, shifting from a constant, unchanging gender, to the middle where gender is defined and scoped to a local level, to the extreme, where gender is variable globally.

[–] [email protected] 1 points 6 months ago

Const keyword means constant, a value that won't change after the application has been compiled; this allows for certain optimizations.

JavaScript does not have compile-time constants (it’s usually not compiled after all). The const keyword declares an immutable variable.

Let keyword is a JavaScript variable that is safely scoped down to the method or function level.

Variables declared with let are scoped to the block, not to the function.

Var keyword is generally discouraged in JavaScript, because it's a global declaration. The value of it could be available anywhere in the application, and the app might have collisions.

var is scoped to the function, not globally (unless it’s at the global level). It is discouraged because of its confusing semantics (“variable hoisting”).