DEV Community

Cover image for Declaring Variables
GiandoDev
GiandoDev

Posted on

Declaring Variables

variable

If we have the number 22 and 12 and we write in our console 22 + 12 javaScript do the math for us and return 34.
Well, but we don't know if 22 and 12 are persons, apples or bricks.
We can do that with variable:

const(key) candiesRed(name of our variable) =(assignment symbol) 12(value)
Enter fullscreen mode Exit fullscreen mode

Like a key we may use const or let, var is not necessary yet.
Like a name we may use anything except Reserved keywords
and it cannot begin with a number.
Like a value we may hold primitives(number, string...), objects(functions, arrays).
Now we can do this:

const redApples = 22;
const yellowApples = 12;
const sum = redApples + yellowApples // Result 34
Enter fullscreen mode Exit fullscreen mode

If a variable name is composed of two or more words we may use camelCase to tie the words together: my red apples become myRedApples.
const key don't allow us to reassign a new value to our variable:

const newValue = 10;
newValue = 43 // returns an error (// ERROR! Cannot assign another value a variable declared with const)

If we want to change the value stored in our variable we have to use let:

let newValue = 10;
newValue = 22; // now the value of our variable is 22 without any error

There is more to talk about the difference from const and let and about changing the value in const, but this is advanced topic, not important now.

Top comments (0)