Comments and Variables and some of the regular features of JavaScript you will not do without throughout your career as a programmer. So for this study, we will be learning about comments and variables.
What are JavaScript Comments?
Disabling code is the simplest thing you can do with JavaScript comments when writing JavaScript programs. You will need words when you do not want to delete the code but want to try a new one in the same file.
For example, let’s say you have two JavaScript programs and wish to prevent one from being read or interpreted by the compiler. Commenting out the line of codes is the solution here.
1, Simple JavaScript code without comment
// in this example we are print an output without using the comment
console.log("With the proper mindset, mentors, and tools, there is nothing you cannot accomplish in life.");
// this line of code initializes x,y,z,sum
let x, y, z, sum;
// assign values the the variables
x = 35;
y = 45;
z = 55;
sum = x + y + z;
// print the output
console.log(sum)
2, Simple JavaScript code with comment
// console.log("With the proper mindset, mentors, and tools, there is nothing you cannot accomplish in life.");
// this line of code initializes x,y,z,sum
let x, y, z, sum;
// assign values the the variables
x = 35;
y = 45;
z = 55;
sum = x + y + z;
// print the output
console.log(sum)
You can also use JavaScript comments to make the code readable. Comments act as guidelines when you are writing code for reuse or when you are working with a team.
// this line of code initializes x,y,z, sum
let x, y, z, sum;
// here we give values to the variables declared above
x = 35;
y = 45;
z = 55;
// This code will add the three values and provide the answer
sum = x + y + z;
// here we ask the computer to print out the answer
console.log(sum)
Types of comments
Single-line and multi-line comments are the two different categories of comments.
1, Single-line comment (//):
A single comment only exempts the code it recognizes on the commented line. Any code found on the following lines is executed.
// this line of code initializes x,y,z,sum
let x, y, z, sum;
// here we give values to the variables declared above
x = 35;
y = 45;
z = 55;
// This code will add the three values and provide the answer
sum = x + y + z;
// here we ask the computer to print out the answer
console.log(sum)
2, Multi-line comment ( /* */):
Multi-line comments are a more comprehensive solution. You can use it to comment out multiple lines in a JavaScript program.
let x, y, z, sum;
// here we give values to the variables declared above
x = 35;
y = 45;
z = 55;
/*
// This code will add the three values and provide the answer
sum = x + y + z;
// here we ask the computer to print out the answer
console.log(sum)
*/
What are JavaScript Variables?
Any programming language you learn will introduce you to the concept of variables. They are known as “containers” or “lightweight databases” for storing data values.
In the examples above, you must have noticed things like y = 45 and x = 35; this is you trying to make y and x represent the values. After assigning these values to these letters, where next you call y or x, you are simply saying 45 and 35.
To declare a variable in JavaScript, you must use some reserved keys. These keywords are:
var
let
const
Using none(these are known as undeclared variables)
So to declare a variable, you must include these keywords, although both function differently.
// this line of code initializes x with let keyword
let x = 34
// this line of code initializes y with var keyword
var y = 23
// this line of code initializes xy with const keyword
const xy = 90
// this line of code initializes without any of the keywords
xyz = 8
As mentioned earlier, there are conditions to using these keywords. Let’s call it a general rule. But before we consider that, which will be in our next tutorial, let’s look at the two types of variables in JavaScript.
a. Local Variable
A variable declared inside a block or function is referred to in JavaScript as a local variable. It is only accessible within the block or function in which it resides.
b. Global Variable
The program as a whole can access global variables, which are declared outside of a function. When more than one function needs to write to an object or access data that is not within the scope of the function, you can use global variables.
Conclusion
In this article, we talked about JavaScript comments and variables and the two types of variables. We’ll examine the keywords var, let, and const in the upcoming tutorial.