14

Guess I'll never understand javascript, same behavior with const

Comments
  • 0
    @J-2FA you tell me
  • 6
    Why would your do that anyway ?
  • 1
    I don't know what are you trying to do
  • 1
    @LuxARTS trying to learn JavaScript
  • 1
    Either you just found a bug or... It let's you overwrite let with a variable name?
  • 1
    @Omarzion it's not overwritten
  • 1
    @J-2FA that, my dear, is called JavaScript
  • 1
    @dextel2 'let' is a reserved keyword so you can't use it as a variable name. It's a bug from interpreter if you don't get an error. It's like write: int char = 1; in C/C++.
  • 0
    @LuxARTS why var can declare it then?
  • 0
    @dextel2 I think the interpreter that you are using has a bug.
    Google "let JavaScript" to get details about it.
  • 1
    Don't complain about programming languages if your attempts to learn that language is garbage input.

    Might aswell override false with true and complain again.
  • 0
    @LuxARTS have you tried repro it?
  • 0
    To people who actually understand JavaScript, this is probably like typing *&_()#-djks and blaming the language for throwing an error.

    Everything in the screenshot is expected behaviour
  • 0
  • 2
    @dextel2 It's not possible to eli5 a concept as complex as this.

    1) First off, check this list of reserved words you shouldn't use as variable names. http://javascripter.net/faq/...

    Both let and var are legal identifiers and should not be used.

    let var
    var let

    Both are wrong.

    2) The difference between let and var is that let is block scoped. It doesn't exist outside the function it's declared in. So let var throws an error, because it tries to create a distinct variable of name "var"

    var can be global scoped and binds to the window object. window is the global root object.

    "var let =1" done globally
    is just shorthand for
    "window.let = 1"

    Since window.let as a whole isn't exactly a legal identifier, there isn't an error, but that doesn't make it right.

    3) I would really recommend you to follow a good standard tutorial or previously written application, if you're looking at learning. Playing around with garbage inputs won't be much productive, because JS being dynamically typed, does a lot of internal work to set variable types. This leads to some non intuitive behavioral, which will take some time to get used to. This is true of all dynamic typed languages, especially one like JS which is sort of a mix of two versions (es5 and es6) in one package.
Add Comment