What is the difference between var and const? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the difference between var and const?

27th Jun 2019, 3:28 PM
Ally Whitmer
Ally Whitmer - avatar
2 Answers
+ 11
const is short for constant, which means the value cannot be changed. A value must be assigned to consts when declared. Ex: const pi = 3.14157; var is short for variable, which means the value can be changed.
27th Jun 2019, 4:03 PM
David Carroll
David Carroll - avatar
+ 6
const is a variable in your code that you definitely don't want to change - for example, you want Pi to always be 3.1415 const myPi = 3.1415 If you try changing or reassigning this value, code won't work properly var however can be changed, that's why it's called a variable. const myPi = 3.1415; myPi = 2; // error var myPi = 3.1415; myPi = 2; // this code works!
27th Jun 2019, 4:31 PM
HNNX 🐿
HNNX 🐿 - avatar