Day 3 of 30 of JavaScript
Tags: javascript, beginners, programming, tutorial
Published: 2024-04-19 16:23:53

Hey curious reader :) I am back with third tutorial on JavaScript.In this post we will see how comments are used and how variables are declared in JavaScript so let's get started .

## 1. Comments in JavaScript -:

First of all let me explain what comments exactly are -:
Comments are meaningful information or messages about code that we use enhance readability of code. We don't want these to be executed along with our code. 
There are two types of comments in JavaScript -:

- **Single line comments** -> These are represented by double slash.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6f1yts8gyx7avxk2epgx.png)

So here as you can lines apart from comments executed successfully.

- **Multi line Comment** -> It can be used to add single as well as multi line comments. So, it is more convenient.
It is represented by forward slash with asterisk then asterisk with forward slash. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3cibthbjw3coxetl9src.png)

## 2. Variable Declaration -:
First of all let's understand what variables are-:
Variable is a symbolic name for a value.
Variables are used to store data that can be referenced and manipulated in a program. 

In JavaScript we can declare variables in three ways ->

1. Using `var` keyword

2. Using `let` keyword

3. Using `const` keyword 
 
Let's see each of the declaration seperately.

## Using `var` keyword

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tnvfr8azuuedb7t6i0yi.png)
Declaring variables with `var` keyword is traditional in JavaScript. These are basically **function scoped** or **global scoped**. Now let's understand what this function and global scoped means.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5bucb2tcwvbe8gd1irlf.png)

As you can see in above image we have created a function and declared a variable `Name` with `var` keyword and used within it and it works perfectly fine, but as soon as, we tried using it outside the function an error is generated. So this clearly specifies that `Name` variable is accessible within function but it is not accessible outside it.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m0e6pelmf5xv8tv6z713.png)
Now here as we have declared `Name` globally it is accessible in whole program.

## Using `let` keyword
Introduced in ECMAScript 2015 (ES6), let allows you to declare block-scoped variables. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d3v44w81kgkq5jharfpu.png)
Now you may be thinking that **"Aren't `var` and `let` same? What's the difference between them?"**
No they aren't same, **`let` is block scoped** whereas **`var` is function scoped**. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cmnxdoto4a6jvuteyu12.png)

> So here as you can see declaring a `let` in block will allow us to use it within that block only, outside of it we can't use it but that's not the case for `var`.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xx0m0wdo4lo0dfojn0h4.png)

> We can redeclare variables with `var` keyword but we cannot do so with `let` keyword.

## Using `const` keyword 
Also introduced in ECMAScript 2015 (ES6), const allows you to declare constants, which are block-scoped like variables declared with let. The value of a constant cannot be reassigned once it is initialized. However, if the constant holds an object or array, the properties or elements of the object or array can still be mutated.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/27hnoh2ypu5u0liumxng.png)

So this is it for this post. I hope you have understood that how declaration is done in JavaScript and how comments are used. In the next post we will see data types. Till then stay connected and don't forget to follow me :)