Dino Geek, try to help you

What is the difference between var let and const in JavaScript?


`var`, `let`, and `const` are all used for variable declaration in JavaScript but they have different usage and scope.

1. `var`: It is function scoped, which means if you declare a variable inside a function using `var`, it cannot be accessed outside of the function. If you declare it outside any function, it becomes a global variable and can be accessed from anywhere in your code. The other thing about `var` is that it is hoisted to the top of its scope, which means you can use `var` variable before it’s actually declared.

1. `let`: It is block scoped, which means that it can only be accessed within the block it was declared. It is not hoisted. `let` allows you to reassign values.

1. `const`: Like `let`, `const` is also block scoped. However, once you assign a value to a `const` variable, you cannot change it (it’s read-only). It is not hoisted. It doesn’t mean the value it holds is immutable, it means that the variable identifier cannot be reassigned.

Here is a brief comparison:

```
// var
var x = 1;
if (true) { var x = 2; // same variable, it’s value is changed console.log(x); // 2
}
console.log(x); // 2

// let
let y = 1;
if (true) { let y = 2; // different variable console.log(y); // 2
}
console.log(y); // 1

// const
const z = 1;
if (true) { const z = 2; // different variable console.log(z); // 2
}
console.log(z); // 1
```


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use