Javascript Const And How To Use It

Tanggal : 15 Feb 2023, Category : JavaScript, Create By : admin

In JavaScript, const is a keyword used to declare a constant variable that cannot be reassigned a new value once it has been initialized. Here's how to use it:

const pi = 3.14;

In this code, pi is a constant variable with the value of 3.14. Once a constant variable has been assigned a value, it cannot be reassigned to a new value:

const pi = 3.14;
pi = 4; // This will result in a TypeError because pi is a constant variable and cannot be reassigned.

Note that const only prevents reassignment of the variable itself, not the value of the object it points to. For example, if you have a const object, you can still modify its properties:

const myObj = { a: 1, b: 2 };
myObj.a = 3; // This is allowed and will change the value of myObj.a to 3.

In addition, const declarations are block-scoped, meaning they can only be accessed within the block they are defined in. For example:

if (true) {
const x = 1;
console.log(x); // This will log 1.
}
console.log(x); // This will result in a ReferenceError because x is not defined outside of the if block.

const is often used for values that should not be changed during the execution of a program, such as constants, configuration settings, or library references. By using const, you can ensure that these values remain constant and avoid accidental reassignment.


Keyword:
  • Javascript Const And How To Use It

Artikel Lain

Menyisipkan Background Gambar Dengan Css

Date : 23 Aug 2018, Category : CSS, Create By : admin

Selamat datang kembali di website gudang ilmu aplikasi tempatnya anda menambah ilmu, kali ini kami akan membahas mengenai menyisipkan atau menambah background gambar dengan css di halaman website anda. oke .....

Pengertian Dan Kegunaan Php Dalam Membuat Aplikasi Website

Date : 28 Jul 2018, Category : Pemprograman Website, Create By : admin

Selamat datang di website gudangilmuaplikasi.com, dalam membuat sebuah aplikasi bserbasis website, tentunnya kita akan sering menggunakan bahasa pemprograman seperti PHP, HTML, CSS, dan Javascript. kali ini gudangilmuaplikasi.com mencoba akan membahas tentang php, apa .....

Menambah Panjang Karakter String Dalam Pemprograman Php

Date : 08 Jan 2019, Category : Tutorial, Create By : admin

Selamat datang di gudang ilmu aplikasi, kali ini kami akan membahas menambah panjang karakter pada sebuah string, hal ini biasanya di gunakan dalam pengkodean sebuah data, atau menambah digit nol .....