DEV Community

royelWilliams
royelWilliams

Posted on

Variables and Keywords

In the coding language JavaScript there are three common variables used; var let and const. These variables can be a great benefit to your code. They can also lead to many complications and issues throughout the code if used incorrectly.

First let's start with the basics of why we need variables when coding. Variables are used to store and or manipulate data. They are able to store any type of data from basic strings to complex functions. They are used as labels to point to the data that will be used itself. You can think of them like the middleman to code. Any variable that you use should be accompanied by a keyword var, let, or const.

Writing a variable can be very easy. First you call your keyword whether it be var, let, or const and following the key word will be whatever variable you desire. A variable can be anything that you want. It can also be written in various ways from camelCase (when the first word is lowercase and every word following it will begin with a capital letter) pascal Case (when every letter of a new word is capitalized including the first word), snake case(where you would add an underscore where spaces would be between words) of course there are other ways you can write a variable like with skewer-case or SCREAMING_SNAKE_ CASE but those ways may be a little less common.

Keywords and Differences

Var was the primary keyword used up until the big JavaScript update in 2015 where const and let were introduced into the program. Let and const made a great change and improvement to JavaScript as you no longer had the issue of messing up code if you used the same variable or if you did not include a variable to go with the keyword.

Image description

VAR
The var keyword can be both globally scoped and local scoped(function scoped).This means that anything that is using the var keyword can be declared both in and out of the global scope. The var keyword can also be redeclared and reassigned. This can create problems in your code later. This can easily lead to one variable being used for different code which can ruin or crash the program you are working with.

var sam = "This is a simple var declaration "
console.log(sam) => "This is a simple var declaration"
var sam = "This is what i am changing it to"
console.log(sam) => "This is what i am changing it to"
//when using the var declaration if you change the code that is attached to the variable things can get messy down the line
Enter fullscreen mode Exit fullscreen mode

LET
The let keyword is more of a function scope(block scope) keyword. A blocks scope is a chunk of code bounded by brackets(curly braces). Anything within curly braces is a block scope. When using this scope if you call anything with the let variable outside of
It will not return any result. It is also important to know that a variable declared with let can be updated within its scope. Unlike var, a let variable can not be re-declared within its scope.

 let dorthy = "A storm is coming " 
//console.log(dorthy) => "A storm is coming"
//let dorthy = "I want to go home"
//console.log(dorthy) => dorthy has already been declared
function todo(tinMan){
   return dorthy 
  let lion = "im scared"
}
//console.log(todo(dorthy)) => "A storm is coming"
console.log(todo(lion)) => lion is not defined
// lion is not defined outside of the function scope
Enter fullscreen mode Exit fullscreen mode

CONST
Like let declarations, const declarations can only be accessed within the block they were declared. So when they are declared outside of their scope they will return undefined. When using the const keyword it is also important to know that it remains the same within its scope. It cannot be updated or re-declared.

const dorthy = "A storm is coming " 
//console.log(dorthy) => "A storm is coming"
const dorthy = "I want to go home"
//console.log(dorthy) => dorthy has already been declared
function todo(tinMan){
   return dorthy 
  const lion = "im scared"
}
//console.log(todo(dorthy)) => "A storm is coming"
console.log(todo(lion)) => lion is not defined
// lion is not defined outside of the function scope
Enter fullscreen mode Exit fullscreen mode

To sum, It is important to understand the connections between keywords and variables as they can really save a code. Understand that by calling different keywords you are opening the gate to allow the code connected to the keyword and variable to be used in various ways. Also recognize the benefits that using different keywords provides as we code.
Happy Coding!!

Top comments (0)