How would I declare a variable with a “+” or “-” sign in it? Javascript










1















I am making a program in Javascript but I am not sure how to declare a variable for grades with + and - in them.



 var A+ = 4.0;
var A = 4.0;
var A- = 3.67;









share|improve this question




























    1















    I am making a program in Javascript but I am not sure how to declare a variable for grades with + and - in them.



     var A+ = 4.0;
    var A = 4.0;
    var A- = 3.67;









    share|improve this question


























      1












      1








      1








      I am making a program in Javascript but I am not sure how to declare a variable for grades with + and - in them.



       var A+ = 4.0;
      var A = 4.0;
      var A- = 3.67;









      share|improve this question
















      I am making a program in Javascript but I am not sure how to declare a variable for grades with + and - in them.



       var A+ = 4.0;
      var A = 4.0;
      var A- = 3.67;






      javascript






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 3:55







      demdragons

















      asked Nov 13 '18 at 1:33









      demdragonsdemdragons

      247




      247






















          1 Answer
          1






          active

          oldest

          votes


















          5














          Those are illegal characters in variable names, but even if they weren't, this sounds like the perfect place for an object (a single variable), rather than multiple standalone variables, and the object can have the key-value pairs you're interested in:



          const grades = 
          'A+': 4,
          'A': 4,
          'A-': 3.67,
          // ...
          ;


          (Note that numbers with trailing decimal zeros have their trailing zeros truncated automatically - if you want to use 4.0, either use a string '4.0' instead, or use toFixed later, when retrieving the number, to convert to a string)



          To iterate over such an object, use Object.entries to get each key-value pair:



          Object.entries(grades).forEach(([key, val]) => 
          // on first iteration, key will be A+, value will be 4
          );


          and to access or set properties on it, put the key names in brackets:



          grades['A+'] = 'newA+Value';


          (dot notation, such as grades.A, only works when the key follows the same rules as valid variable names - otherwise, have to use bracket notation)






          share|improve this answer























          • When attempting to use Object.entries, I get the same issue I had trying to declare the variables originally. It will not read it as "A+", separating the + from the A.

            – demdragons
            Nov 14 '18 at 2:39











          • In the first iteration of Object.entries, the key will be a string containing A+, for example, which is perfectly valid - then, just use that string (and its value) for whatever you need it for.

            – CertainPerformance
            Nov 14 '18 at 2:41











          • Okay, thank you. My last question would be how would I use this in a function, for example, if I used document.getElementById("grade1").value to get the inputted value from the textarea, say it was an A+. Would setting up these constants automatically turn that input into 4? Sorry for the confusion, I am just starting out with programming.

            – demdragons
            Nov 14 '18 at 3:13











          • See the last part of the answer - to access or set properties on the object, just put the key names in brackets. eg if value contains the A+ string, then you can get the number by doing grades[value]

            – CertainPerformance
            Nov 14 '18 at 3:15










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272492%2fhow-would-i-declare-a-variable-with-a-or-sign-in-it-javascript%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5














          Those are illegal characters in variable names, but even if they weren't, this sounds like the perfect place for an object (a single variable), rather than multiple standalone variables, and the object can have the key-value pairs you're interested in:



          const grades = 
          'A+': 4,
          'A': 4,
          'A-': 3.67,
          // ...
          ;


          (Note that numbers with trailing decimal zeros have their trailing zeros truncated automatically - if you want to use 4.0, either use a string '4.0' instead, or use toFixed later, when retrieving the number, to convert to a string)



          To iterate over such an object, use Object.entries to get each key-value pair:



          Object.entries(grades).forEach(([key, val]) => 
          // on first iteration, key will be A+, value will be 4
          );


          and to access or set properties on it, put the key names in brackets:



          grades['A+'] = 'newA+Value';


          (dot notation, such as grades.A, only works when the key follows the same rules as valid variable names - otherwise, have to use bracket notation)






          share|improve this answer























          • When attempting to use Object.entries, I get the same issue I had trying to declare the variables originally. It will not read it as "A+", separating the + from the A.

            – demdragons
            Nov 14 '18 at 2:39











          • In the first iteration of Object.entries, the key will be a string containing A+, for example, which is perfectly valid - then, just use that string (and its value) for whatever you need it for.

            – CertainPerformance
            Nov 14 '18 at 2:41











          • Okay, thank you. My last question would be how would I use this in a function, for example, if I used document.getElementById("grade1").value to get the inputted value from the textarea, say it was an A+. Would setting up these constants automatically turn that input into 4? Sorry for the confusion, I am just starting out with programming.

            – demdragons
            Nov 14 '18 at 3:13











          • See the last part of the answer - to access or set properties on the object, just put the key names in brackets. eg if value contains the A+ string, then you can get the number by doing grades[value]

            – CertainPerformance
            Nov 14 '18 at 3:15















          5














          Those are illegal characters in variable names, but even if they weren't, this sounds like the perfect place for an object (a single variable), rather than multiple standalone variables, and the object can have the key-value pairs you're interested in:



          const grades = 
          'A+': 4,
          'A': 4,
          'A-': 3.67,
          // ...
          ;


          (Note that numbers with trailing decimal zeros have their trailing zeros truncated automatically - if you want to use 4.0, either use a string '4.0' instead, or use toFixed later, when retrieving the number, to convert to a string)



          To iterate over such an object, use Object.entries to get each key-value pair:



          Object.entries(grades).forEach(([key, val]) => 
          // on first iteration, key will be A+, value will be 4
          );


          and to access or set properties on it, put the key names in brackets:



          grades['A+'] = 'newA+Value';


          (dot notation, such as grades.A, only works when the key follows the same rules as valid variable names - otherwise, have to use bracket notation)






          share|improve this answer























          • When attempting to use Object.entries, I get the same issue I had trying to declare the variables originally. It will not read it as "A+", separating the + from the A.

            – demdragons
            Nov 14 '18 at 2:39











          • In the first iteration of Object.entries, the key will be a string containing A+, for example, which is perfectly valid - then, just use that string (and its value) for whatever you need it for.

            – CertainPerformance
            Nov 14 '18 at 2:41











          • Okay, thank you. My last question would be how would I use this in a function, for example, if I used document.getElementById("grade1").value to get the inputted value from the textarea, say it was an A+. Would setting up these constants automatically turn that input into 4? Sorry for the confusion, I am just starting out with programming.

            – demdragons
            Nov 14 '18 at 3:13











          • See the last part of the answer - to access or set properties on the object, just put the key names in brackets. eg if value contains the A+ string, then you can get the number by doing grades[value]

            – CertainPerformance
            Nov 14 '18 at 3:15













          5












          5








          5







          Those are illegal characters in variable names, but even if they weren't, this sounds like the perfect place for an object (a single variable), rather than multiple standalone variables, and the object can have the key-value pairs you're interested in:



          const grades = 
          'A+': 4,
          'A': 4,
          'A-': 3.67,
          // ...
          ;


          (Note that numbers with trailing decimal zeros have their trailing zeros truncated automatically - if you want to use 4.0, either use a string '4.0' instead, or use toFixed later, when retrieving the number, to convert to a string)



          To iterate over such an object, use Object.entries to get each key-value pair:



          Object.entries(grades).forEach(([key, val]) => 
          // on first iteration, key will be A+, value will be 4
          );


          and to access or set properties on it, put the key names in brackets:



          grades['A+'] = 'newA+Value';


          (dot notation, such as grades.A, only works when the key follows the same rules as valid variable names - otherwise, have to use bracket notation)






          share|improve this answer













          Those are illegal characters in variable names, but even if they weren't, this sounds like the perfect place for an object (a single variable), rather than multiple standalone variables, and the object can have the key-value pairs you're interested in:



          const grades = 
          'A+': 4,
          'A': 4,
          'A-': 3.67,
          // ...
          ;


          (Note that numbers with trailing decimal zeros have their trailing zeros truncated automatically - if you want to use 4.0, either use a string '4.0' instead, or use toFixed later, when retrieving the number, to convert to a string)



          To iterate over such an object, use Object.entries to get each key-value pair:



          Object.entries(grades).forEach(([key, val]) => 
          // on first iteration, key will be A+, value will be 4
          );


          and to access or set properties on it, put the key names in brackets:



          grades['A+'] = 'newA+Value';


          (dot notation, such as grades.A, only works when the key follows the same rules as valid variable names - otherwise, have to use bracket notation)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 1:35









          CertainPerformanceCertainPerformance

          84.8k154169




          84.8k154169












          • When attempting to use Object.entries, I get the same issue I had trying to declare the variables originally. It will not read it as "A+", separating the + from the A.

            – demdragons
            Nov 14 '18 at 2:39











          • In the first iteration of Object.entries, the key will be a string containing A+, for example, which is perfectly valid - then, just use that string (and its value) for whatever you need it for.

            – CertainPerformance
            Nov 14 '18 at 2:41











          • Okay, thank you. My last question would be how would I use this in a function, for example, if I used document.getElementById("grade1").value to get the inputted value from the textarea, say it was an A+. Would setting up these constants automatically turn that input into 4? Sorry for the confusion, I am just starting out with programming.

            – demdragons
            Nov 14 '18 at 3:13











          • See the last part of the answer - to access or set properties on the object, just put the key names in brackets. eg if value contains the A+ string, then you can get the number by doing grades[value]

            – CertainPerformance
            Nov 14 '18 at 3:15

















          • When attempting to use Object.entries, I get the same issue I had trying to declare the variables originally. It will not read it as "A+", separating the + from the A.

            – demdragons
            Nov 14 '18 at 2:39











          • In the first iteration of Object.entries, the key will be a string containing A+, for example, which is perfectly valid - then, just use that string (and its value) for whatever you need it for.

            – CertainPerformance
            Nov 14 '18 at 2:41











          • Okay, thank you. My last question would be how would I use this in a function, for example, if I used document.getElementById("grade1").value to get the inputted value from the textarea, say it was an A+. Would setting up these constants automatically turn that input into 4? Sorry for the confusion, I am just starting out with programming.

            – demdragons
            Nov 14 '18 at 3:13











          • See the last part of the answer - to access or set properties on the object, just put the key names in brackets. eg if value contains the A+ string, then you can get the number by doing grades[value]

            – CertainPerformance
            Nov 14 '18 at 3:15
















          When attempting to use Object.entries, I get the same issue I had trying to declare the variables originally. It will not read it as "A+", separating the + from the A.

          – demdragons
          Nov 14 '18 at 2:39





          When attempting to use Object.entries, I get the same issue I had trying to declare the variables originally. It will not read it as "A+", separating the + from the A.

          – demdragons
          Nov 14 '18 at 2:39













          In the first iteration of Object.entries, the key will be a string containing A+, for example, which is perfectly valid - then, just use that string (and its value) for whatever you need it for.

          – CertainPerformance
          Nov 14 '18 at 2:41





          In the first iteration of Object.entries, the key will be a string containing A+, for example, which is perfectly valid - then, just use that string (and its value) for whatever you need it for.

          – CertainPerformance
          Nov 14 '18 at 2:41













          Okay, thank you. My last question would be how would I use this in a function, for example, if I used document.getElementById("grade1").value to get the inputted value from the textarea, say it was an A+. Would setting up these constants automatically turn that input into 4? Sorry for the confusion, I am just starting out with programming.

          – demdragons
          Nov 14 '18 at 3:13





          Okay, thank you. My last question would be how would I use this in a function, for example, if I used document.getElementById("grade1").value to get the inputted value from the textarea, say it was an A+. Would setting up these constants automatically turn that input into 4? Sorry for the confusion, I am just starting out with programming.

          – demdragons
          Nov 14 '18 at 3:13













          See the last part of the answer - to access or set properties on the object, just put the key names in brackets. eg if value contains the A+ string, then you can get the number by doing grades[value]

          – CertainPerformance
          Nov 14 '18 at 3:15





          See the last part of the answer - to access or set properties on the object, just put the key names in brackets. eg if value contains the A+ string, then you can get the number by doing grades[value]

          – CertainPerformance
          Nov 14 '18 at 3:15

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272492%2fhow-would-i-declare-a-variable-with-a-or-sign-in-it-javascript%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          Museum of Modern and Contemporary Art of Trento and Rovereto

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully