Creating Values in JS
As I had discussed in the previous post, a program uses values stored in the system to work on. Such values are called variables.
Binding means to create and hold values, also called variables.
var name = 'Dash';
We did the same with the age, but this time saving a number datatype in it. *//* Now, we have 2 variables to operate on. */
Binding names- If you look closely above, you would notice that all the values have a name, why? Simple. If you store ‘Dash’, and want to use it later on, how will you know which value is ‘Dash’. Computer stores all the data in 0s & 1s. Just for the sake of human understandability, we have designed JS like this.
So, how do we think of a name and create a data? Good question.
Bad naming conventions can lead to confusion and chaos, resulting in bugs, errors and frustration. The following are the rules for naming a variable-
- Can contain alphanumeric, $ and _ only, no spaces allowed
- Can NOT start with a digit.
- Can NOT use reserved keyword.
Good Naming Convention
Look at the following code snippet
var fullnameofuser = 'Sudesh Kumar Das';
var full_name_of_user = 'Sudesh Kumar Das';
var fullNAMEofUSER = 'Sudesh Kumar Das';
var fullNameOfUser = 'Sudesh Kumar Das';
All of them are different variables. A single change in the case or a symbol leads to creation of new variable. But look at the names, which one is the easiest to read? If your answer is 2nd one, good, you have a perfect eye. But it is quite difficult to write.
Thats why it is a custom now among programmers to use the last one, easy enough to read, and not to harsh on hands when typing.
This is known as camelCase, the first letter of first word is lowercase, and every subsequent word’s first letter is uppercase.
“Awesome! Now I know how to name my variable. But how do I create them?”
- var keyword- Short for variable, it is quite self explanatory.
- let keyword- A new way of creating variable, which was introduced in ES6.
- const keyword- Another introduction in 2015 when ES6 was released.
“Wait, what are the differences between all these?” var and let quite similar, having subtle differences which are a level above we are learning now. We’ll get to them in future.
The main difference we should know now is that the value of variables created using ‘var’ and ‘let’ keywords can be changed in the course of code, whereas variables declared using ‘const’ are constant, i.e, they CAN NOT be changed once created.
const name = 'Dash'; // can not be changed
var age = 24; // can be changed
let feel = 'Happy'; // can be changed// We have created the values, now lets try to change their value.name = 'Sudesh'; // will throw an error
age = 19; // variable age has now value of 19
feeling = 'Excited' // same as age, 'Excited' is now the value
Now we know how to create variables, but when to use what? Simple, create variables which may change such as age, feeling using ‘let’ and ‘var’ and those who stay constant with ‘const’ such as name.
Closing thoughts- We now understand how to create data, give them the name and store them in the system.