Let's talk about Variables

Today's blog post is all about "javascript basics". In subsequent posts, I will be breaking down a couple of javascript concepts, methods, types, and functions in simple terms so that everyone reading this can learn 1 or 2 things about Javascript from me. This is also a way for me to document some JS topics that I can always come back to when I need a refresher. With no further ado, let's get started.

Javascript is one of the most popular and powerful programming languages. I have been using javascript for over 3 years now and guess what? I'm still learning as each day passes. I will be starting my series with one of the most important topics in every programming language which is variables.

Let's talk about Variables 

The question I always answer when trying to understand/ break things down is "How do I explain this to a 5-year-old"? or "How will a non-programmer understand this"? This is also the approach that will be used to explain most of the topics in this series. 

So how will I explain the term "variables" to a non-programmer friend? 

Well, I would tell my friend that a variable is a container that stores information. Think of a variable as a box that contains information. You can open the box and replace the information, add something else to the box, or totally remove the information. 

In computer programming, a variable has a name and a value. With my definition of variables above, if we relate that to the box. Let's look at a simple example. Think about a box of fruits with different fruits like apples, bananas, and grapes. You can call the box of fruits (variable name) and each fruit can be given a name (value)

Two important things to note about Variables

Variable naming: 

 » There are two limitations on variable names in JavaScript.

  • The name must contain only letters, digits, or the symbols $ and _.
  • The first character must not be a digit.

» A variable name should have a clean, obvious meaning, describing the data that it stores.

  • When the name contains multiple words, camelCase is commonly used e.g userName
  • Use human-readable names like currentTemperature, newPassword, etc.
  • Avoid abbreviations or short names like x, y, and z.
  • Let your variable names be descriptive and concise. 
» A variable should be declared only once. A repeated declaration of the same variable will lead to an error (syntax error)
  • In Javascript, all variables are case-sensitive, meaning that GRAPES is not the same as grapes.

    Ways to Declare Variables
    • Before you can use a variable, you have to create it (this is called declaring a variable).
    • A variable should be declared only once. A repeated declaration of the same variable will display an error (syntax error).
    • In JavaScript, a variable can be declared using var, let, and const keywords.
      • var keyword was used to declare variables when JavaScript was first created. This was the only way to declare variables at that time. This keyword can be confusing and also error-prone.
      • let keyword removes the confusion and error of var. It works somewhat differently to var, fixing its issues in the process. It is also the new and recommended way of declaring variables in JavaScript. The let keyword allows you to declare variables that are limited to the scope of a block statement, or expression on which it is used, unlike the var keyword, which declares a variable globally, or locally to an entire function regardless of block scope.
      • const keyword is used to declare a constant variable that cannot be changed once assigned a value.

    Wrapping Up
    Javascript variables are used to hold values and these values can be reassigned (if need be) by using the right keyword. Also, don't forget to always give a good name to your variables when declaring them. I hope this article has helped understand variables better and you also learned one or two things from me today. That will be all from me today. I will see you in the next one. Bye👋

    Quote:- "Life is a variable, only change is constant." ~ Lokesh Kumar Pawar

    HAPPY CODING!!! ❤️