Creating Values in JS

#teamtanayejschallenge- Part 2- Binding & creating data in JS

Sudesh Kumar Das
3 min readDec 5, 2020
Photo by James Harrison on Unsplash

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

Binding means to create and hold values, also called variables.

var name = 'Dash';
var age = 19;
/* Here, we told JavaScript to create a data by the name of 'name' and store a value 'Dash' of string datatype.
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.

“What are reserved keywords?” You ask. JavaScript is an ever-growing and dynamic language, with revised versions with updates coming up every now and then by the approval of ECMAScript. Some words are reserved which either has a special meaning known as keywords (‘break, alert, function’) or are kept for the new features to arrive. Do keep in mind not to use these.

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.

Creating variables

“Awesome! Now I know how to name my variable. But how do I create them?”

There are three ways of creating varibales in JavaScript.

  • 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.

That is it, for details of the challenge. Click Here. This article was inspired by Eloquent JavaScript- Chapter 2 by Marijn Haverbeke.

--

--