locked
when and where to declare const. RRS feed

  • Question

  • User2102072086 posted

        Q1) i want to use const of javascript and let with jquery 1.9 on my page , can I use it?

        Q2) I have more than one js file on my aspx page, what I have noticed is that in each js file , I have some variables which are declared with var but they are nothing but abbreviation(which are not changed). so can I make them const?

    Tuesday, June 30, 2020 10:21 AM

All replies

  • User475983607 posted

    Creating a const in global scope is very simple and take a few seconds to test. 

    Are you receiving an error?  Is there anyway you can share code that illustrates the problem? 

    Tuesday, June 30, 2020 10:38 AM
  • User753101303 posted

    Hi,

    jQuery is a JavaScript library. const and let are part of JavaScript itself. See perhaps  https://caniuse.com/#search=let or https://caniuse.com/#search=const  to see if it matches your supported browsers. If those values are never reassigned using const seems fine. Try perhaps https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const

    If you tried something, it will be easier to just discuss the problem you have rather than going back at general questions about const...

    Tuesday, June 30, 2020 11:57 AM
  • User-857013053 posted

    jQuery is JavaScript library, It is not language, and it doesn't have constants. You can only create an imaginary constant, with var. which will differ from variables..

    Wednesday, July 1, 2020 4:58 AM
  • User-474980206 posted

    ifour.parth@gmail.com

    jQuery is JavaScript library, It is not language, and it doesn't have constants. You can only create an imaginary constant, with var. which will differ from variables..

    modern javascript has constants:

      const HELLO = "hello";
      HELLO = "hi"; // runtime error

    it also add  the let keyword, that allows block scope, unlike the var which has function scope

    function a() {
       var a = 1;
       {
          var a = 2;
       }
       return a;  //2
    }
    
    function b() {
       let a = 1;
       {
         let a = 2;
       }
       return a;  //1
    }

    Thursday, July 2, 2020 7:43 PM