DEV Community

Akshat Sharma
Akshat Sharma

Posted on

Day 3 of 30 of JavaScript

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

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

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

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

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

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

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 :)

Top comments (3)

zaselalk profile image

keep learning !

elon profile image
Sulaiman jamilu

Wow so grateful

thoufiqizhar7 profile image
N Mohammed Thoufiq Izhar

Wow great Akshat ๐Ÿ˜Š