Variables(Var, Let and Const) and Functions in JavaScript…. 30daysofjavascript => Day 2

Olajide Blessing Niniola
3 min readMar 5, 2021

Variables

JavaScript variables are seen as a way of storing data. All variables must be identified with unique names, these unique names are called identifiers. Identifiers can be short such as (a and b) or even more expressive such as (age, firstName, typeOfAccount). Rules to follow when writing variables include:

  • Names can contain letters, digits, underscores, and dollar signs.
  • Names must begin with a letter
  • Names are case sensitive (y and Y are different variables)
  • Reserved words cannot be used as names

Variables can be declared using the var, let and const. Learn more about these keywords. In this unit, we are going to be using more of let but I will start with var to explain basic declarations.

Declaring, re-declaring and arithmetic in variables

A simple variable declared

We have just declared a variable, how easy!!! Now let us break it down; var is a statement used to declare a variable, firstName as seen in the code snippet is the variable name while ‘Blessing’ is the value.

One statement, several variables

As much as possible we are trying to write less but effective codes. Luckily JavaScript allows us to do that (check the snippet above).

Arithmetic in variables and redeclaring variables

Arithmetic in Variables is quite easy, this involves adding two or more variable values to give the desired result. For instance, after you must have declared a variable firstName, middleName and lastName, there might be a certain way you want to put that information e.g. lastName firstName middleName, in the case you use the operator signs to bring them together. PS: You can add white space by including an empty ‘ ’. Check the code snippets above.

Lastly, variables can be re-declared which means they can be given another value and it won’t still disrupt the code.

Functions

A function is a block of code designed to perform a particular task, it is only executed when it is called upon. For example, a function can be set to execute when a certain event occurs such as on click, on load, on drag etc. A function is said to be unique because it can be reused several times and can produce different results depending on how it is called.

A simple function
Arithmetic operation in a function

Here the function is merely invoked by the operator (). The function can also be used to perform arithmetic operations. Click to read more on functions.

Tips

  • Arithmetic operations in variable include are +, *, -, % and /

Personal Experience

I remember the first time I tried to code in JavaScript, function was one of the big issues for me. I just did not know why I do not understand it but it wasn’t a pleasant experience. In this episode, I have been able to learn what functions are and how they can be called and reused and also about variables as well as the scope of variables. Overall, I can say I have learnt a lot in this episode.

Read my last episode here.

--

--