Q:

javascript variable showing unexpected result

Why I'm getting a ReferenceError: can't find variable lang1 in the console?
If lang exists shouldn't lang1 exist too?...
<body>
    <input id="datetimepicker" type="text" placeholder="Datetimerpicker">
        <input id="lang" type="text" placeholder="language" value="en"><div class="select">select language</div>

    <script>
     $(".select").click(function(){
        var lang = $('#lang').val();
        var lang1 = lang;
    });
    </script>
</body>

And shouldn't lang = "en"? I'm getting lang = <input id=​"lang" type=​"text" placeholder=​"language" value="en">​ in the console

A:

If you want to use the browser debugging tools for your function, you have to do things the way the tools intend.  Just typing lang and lang1 into the console command prompt won't really do what you want. In this case, the symbol lang will refer to the global symbol created by implication from your use of lang as the "id" of the <input> field.
You can use the tools to set a breakpoint in your event handler. One easy way is to add a debugger statement to the start of the "click" handler:
 $(".select").click(function(){
    debugger;
    var lang = $('#lang').val();
    var lang1 = lang;
});

From that point, the debugging tools will provide a way for you to look at the values of variables in the active function (and the functions that got you there).

A:

You can not reference the lang and lang1 in the console since they are local to the click's closure. The reason why lang is defined in the console is it will use any element id that matches your variable. Change the variable to langX and you will have the same error.

