Skip to main content

How to change const variable value in javascript

JavaScript const variable value change

As you known, Variable declare with const keyword you can not change later in program, but this condition is apply only for constant primitive values not for constant object or array.

Note : If you declare constant object or array value with const keyword, You can change or add the properties of const variables.

Let's see the examples:

 ➤ Example 1 : constant primitive values;

  • If we assign a primitive value to a constant, we can't change the primitive value.

  1. <script>
  2. const x = 25;//output : 25

  3. x++; //ERROR, not allowed

  4. x = 20; //ERROR, not allowed

  5. x = x+5; //ERROR, not allowed

  6. </script>

# Let see how to change the value of const variable

 ➤ Example 2: constant objects values;

  • You can change the properties of a constant object.

  1. <script>
  2. //You can create a const object.

  3. const student = {name:"Ankaj", branch:"CS"};

  4.  

  5. //You can change a property.

  6. student.name = "Ankaj Gupta"; //Allow

  7.  

  8. //You can add a property.

  9. student.rollno = "45"; //Allow

  10.  

  11. //Note : But you can't re-assign a constant object.

  12. student = {name:"Anmol", branch:"initialize"}; //ERROR, not allowed

  13. </script>

 ➤ Example 3 : constant array values;

  • You can change the elements of a constant array.

  1. <script>
  2. //You can create a const array.

  3. const student = ["Name", "Branch", "Rollno"];

  4.  

  5. //You can change an element.

  6. student[2] = "Enroll_No"; //Allow

  7.  

  8. //You can add an element.

  9. student.push("Gender"); //Allow

  10.  

  11. //Note : But you can't reassign a constant array.

  12. student = ["Name", "Branch", "Enroll_No", "Gender"];//ERROR, not allowed

  13. </script>

Comments

Popular Posts

Django static files not working when debug false || debug true

# Django static and media files not working when debug is false In this article you will learn how to fix problem of not loading static files and media in Django even the DEBUG is FALSE. This is the easiest and safest solution. # Problem: Django static and media files not working when debug is false  ➤ Code: settings.py DEBUG = False #True ALLOWED_HOSTS = [ '*' ] #Host name # Problem Fix: Let's see, How you can fix the problem of Django static and media files not working when DEBUB = False : 1.)First way: devserver in insecure mode If you still need to server static locally ( e.g. for testing without debug ) you can run devserver in insecure mode: python manage.py runserver --insecure --insecure: it means you can run serve

java program for student details using inheritance

# Java program to print student details using "single-level" inheritance In this section, You will learn how to print student details using single inheritance in java, with Scanner and without Scanner class. 1.) With " Scanner " class Let's try to create a simple example :   ➤ Example : student.java; import java.util.Scanner; class StudentDetails {    int roll_no ;    String name , cl ; //creating a function to take student details    void input () { Scanner sc = new Scanner ( System . in );   System . out . print ( "Ente

How to remove the date and .html from every blogger post url

#Remove date and .html from blogger post url A Common search term which every blogger search is How to Remove Date From Blogger Post URL or how do I remove date from blogger permalink? Follow the steps below and then date and .html will be removed from the URL of your blogger post. Step 1 : Login to your Blogger blog and select Theme / Template. Step 2 : Click on Edit HTML and paste the below code just above the </head> tag let's see code :   ➤ Code : mycode.js; Copy code <script type='text/javascript' > //<![CDATA[ // BloggerJS v0.3.1 var urlTotal,nextPageToken,postsDatePrefix=!1,accessOnly=!1,useApiV3=!1,apiKey="",blogId="",postsOrPages=["pages","posts"],jsonIndex=1,secondRequest=!0,feedPriority=0,amp="&"[0];function urlVal(){var e=window.location.pathname,t=e.length;return