Var, const and let in javascript

Is there any reason to use var instead of const and let? seriously, why do i still see var everywhere? whats the point?

Attached: Screen Shot 2018-05-16 at 22.37.32.png (260x294, 68K)

Other urls found in this thread:

devdocs.io/javascript/statements/const
github.com/appcypher/awesome-wasm-langs
developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const
twitter.com/NSFWRedditVideo

Var is useless in ecma6

Let was introduced in ecma6 so it's relatively new. Ecma5 needs babel to translate it

>Is there any reason to use javascript instead of lierally anything else? seriously, why do i still see javascript everywhere? whats the point?
ftfy

legacy and/or brainlets who haven't learned modern JS.

>why hasn't every line of code ever written prior to 2015 been re-written to take advantage of ES6
2/10 bait

I use var all the time.
Explain to me why I should be using const and let instead.
Pls no bully, it'll only make me use var more.

too much old code c-c c-v 'd

Not OP but I still see this in a lot of new projects/fresh code.

If you don't know which to use then don't use any. It'll determine which is the most appropriate automatically at runtime.

Ok, well if it's newly written code beat the person until they stop moving and then show them how to Google "what do all these warnings in my IDE mean"

>using an IDE

Because using var means that variables arent always declared in the place where they are declared... which causes weird behaving code. See below
function opisafaggot() {
//num's deceleration is moved to the top of the function
for(let i=0; i

it's called functional scoping you fucking retard

>what do all these warnings in eslint mean
This works for poorfag neets who can't afford an IDE too.

I thought variable hoisting was just one of those things you had to keep in mind while using javascript.

For the most part it is. You have to be writing utterly retarded pajeet-tier shit to ever have variable hoisting actually bite you in the ass. You should still use const when you can, though.

So how is let different than this? No hoisting?

>You should still use const when you can, though.
Why? Is there even a single JIT that generates faster code with 'const' bindings than 'let'?

Yes. Let is block scoped, which means the variable will only be visible from within the same scope its declared in. It behaves much more like other programming languages (like Java). If you declare a variable inside of a loop using let, you can't access that variable outside of the loop.
Why do people immediately assume the only reason to ever do something is performance? When will people stop worrying about micro-optimizations and realize that real-world applications are slow because of bad architecture and misconfigured infrastructure, not because the JIT didn't optimize away a couple of instructions?

Name one other scripting language supported by most modern browsers that you can use to control web pages.

>Is there any reason to use var instead of const and let?
No
>why do i still see var everywhere?
let and const are new language features and older browsers only support var

>Why do people immediately assume the only reason to ever do something is performance?
Why else would you frivolously use const? Just to add extra restrictions to what you can write?

this lol. OP should've added
>whats the point of var and const when you can just use let

I'm bullying you.

Var was the all encompassing variable prior to ES6.

Const defines a variable that should be immutable.

Let defines a variable that is mutable.

Just allows for better clarification and some cleaner code overall.

>Const defines a variable that should be immutable.
Really? Then why can I do this?

[/code]
const object = {foo: 'bar'}
object.foo = 'skub'
object.new_property = 42

const array = []
array.push(object)
[/code]

Attached: b36.png (420x420, 12K)

>[/code][/code]

if i use let instead of var in a for loop do i still have to deal with closures?

devdocs.io/javascript/statements/const

Attached: 1507484846491.jpg (645x729, 26K)

Because the "object" variable is a reference to that object. You have not mutated that reference by modifying the object it refers to.

this is why you should learn vanilla js before jumping into meme frameworks

Yes, if you're still declaring functions in that loop

let and const are a part of es6, which is 'vanilla' javascript. you don't need babel to use them these days.

always use let/const
set up eslint rule to disallow var

you should never use var

>why do i still see var everywhere? whats the point?
it predates let and const

and that is pretty stupid.

```
if (true) {
var foo = true
}

console.log(foo)
```

>variables arent limited to the scope they were declared in
js/dynamic typing was a mistake

>language already provides a way to declare variables
>for no good reason make up a whole new unnecessary way to declare variables that no one asked for
>look surprised when nobody uses it

let basically var, but less retarded.

const is a joke.

t. Unemployed pajeet

y does this matter lol
ive used var all the time and never had a problem

PHP

retarded answer to an even more retarded question

Attached: 1511458232531.png (235x215, 5K)

var looks better
let is useless
i use const more than either

>let is useless
what

VAR = function-scoped, can be re-declared.
LET = block-scoped, can be re-declared.
CONST = block-scoped, cannot be re-declared.

That's the only difference OP.

Just look at it. Long noodly letters instead of the strong clear ones of var.
Don't want it anywhere near my code.

/thread

the absolute state of web dev

If the spec changes assignment operator to be then I'm done.

var there = "light";
// let there be "light";

Can we just talk for a second why const is retarded?

If I declare an object as const, why can I mutate it? who thought that was a good idea?
const just mean unmutable?

I get JS was designed on 13 days, but there is excuse such shitty things make it to language these days.

WASM

there's also
(NOTHING) = global
which is a pretty good reason to use var at least

No you're just retarded and don't understand reference types.

>github.com/appcypher/awesome-wasm-langs
>literally nothing of value available yet
My sides.

Const means the object reference can't be reseated, not that the object is immutable. Const guarantees that the variable will refer to the same object for its lifetime, not that the variable won't change. It is no difference to final in Java or const pointers in C or C++.

Uh oh, loser alert...

*Not that the object won't change.

You mean the value of the object?

Correct. The object's identity remains constant, not its contents.

Of course let makes more sense, but

1. I can type var with a single hand
2. It's more comfy and ubiquitous because other major languages (Java, C#) have it too
3. Side effects of var only happen when you write deliberately bad code, which only beginners do. I'm no beginner any more so the side effects don't worry me. It's the same thing with comparing different types. JS is a mess at this but it doesn't bother me because I no longer write bad code like that. Working with statically typed languages has made me proficient.

Whining about var/let/const is pointless as long as you can still compare [ ] with false, { } with 4, and overwrite prototype functions because you stayed up late on a coffee induced ADHD session, forgetting to commit to your git repo and now having to manually rewrite all the shit you fucked up since the day before.

Let the name calling begin.

Attached: 1449190818238.jpg (290x290, 37K)

it's fast, quick to write, concise and aesthetic
These are only 2 years old and the benefits are quite minor

const was designed in a few years

Attached: 1524941557999.jpg (960x587, 128K)

/thread

For once, basically every language compiles to js these days.

There is absolutely ZERO reasons to use JS.

>Const defines a variable that should be immutable.
Not at all...
>The const declaration creates a read-only reference to a value. It does not mean the value it holds is immutable, just that the variable identifier cannot be reassigned.

developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const

Please take a look at:

I agree it would be nicer if it were immutable.

Thankfully there is the Immutable.js library.
>import { Map } from 'immutable'
>const changeMeBitch = new Map({ change: 'i don\'t think so'});

Or you can just do Object.freeze().

Problem is: Frozen objects are pretty much useless.
Immutable objects have useful functions like filter, map, sort, reduce, etc. etc.

>understood promises very well
>tfw too much of a brainlet to understand observables
Why did they change everything? Why did rename then to map and subscribe? Why are there so many fucking functions. What the fuck does next do? What the fuck does takeWhile do?

>why do i still see var everywhere
undereducated newbs and legacy code

>using a plain text editor
and you wonder why your projects end up with shit code

>Just to add extra restrictions to what you can write?
YES
because that is how you write code that is PROVABLY correct and works as intended. Leaving unnecessary possibilities in your code invites you and people working with your code ti make mistakes and not even realize. Properly written code with the correct restraints in place creates a system where you simply cannot write wrong code since it wouldn't compile.

You might as well say "hurr why do we declare fields in a class as private with a getter, it's so much easier if you just make the field public"

those 3 arguments are all pretty non-technical and slippery slope, user