hello guys welcome to the next blog on JavaScript tutorial for beginners in this blog we will learn about comments and variables in JavaScript so let's get started with comments you can give comments in JavaScript in two ways one is a single line comment and you can give a single line comment by giving two forward slashes and then your comment for example my code okay so this is how you can give a single line comment and whatever you write after these two double forward slashes will not be executed by your JavaScript code or you can give a comment by giving these forward slash and then asterisks and then Asterix and forward slash okay and whatever comment you will give in between this forward slash and Astrix and Astrix and forward slash it will be considered as a comment so if you want to give a multi-line comment you can give something like this okay so this is how you can use comments in JavaScript now let's talk about variable so first of all what is a variable a variable is a container which can contain some data so for example we can assign a variable or we can name a variable like X and then we can assign some data for example one into it right so X is a variable which contains the value of one now in JavaScript to declare our variables there are some rules okay so there are mainly I think three or four rules you need to remember whenever declaring the variable first rule is you can declare a variable using numbers letters a dollar symbol or an underscore okay so you can declare a variable something like my variable or my variable something like this okay your variable name can also start with a dollar symbol so for example dollar my variable or you can also start your variable name by underscore like this my variable okay you can also declare a variable using a number after a letter or a keyword for example you want to declare X one you can declare a variable but a variable cannot start with a number so this is not valid okay so X 1 is valid but 1x is not valid because your variable cannot start with a number the next rule is your variable cannot contain the reserved keywords in JavaScript so there are a number of keyword which are reserved in JavaScript and with this course when we learn more we will learn more about these keywords for example there is a keyword called var or there is a keyword called function these are reserved keywords and you cannot declare a variable using these keywords ok now I will show you some more interesting stuff so if you have a Google Chrome browser just open your Google Chrome browser and then right-click on it and then go to the last option which says inspect element ok and this is the thing which will help you to debug and find the errors into your JavaScript code so for example go to console here and in here for example I will declare a variable called X is equal to 1 and press Enter and it will give you the result 1 something like this I will make it a little bit bigger so it will give you the result 1 right or for example you can give a variable called Y is equal to five and it will give you the result five now you have two variables x and y which you can use in your console here so for example X plus y when you enter it will give you 6 because X is equal to 1 and Y is equal to 5 and when you add these two it will give you 6 right now I have told you that our variable cannot start with a number so let's check here if this variable when we declared with a number will it pass or not so 1 my well for example ok we'd start with a number and press ENTER and it will give you this error which says uncaught syntax error right so this is not allowed but when we declare a variable something like my well and assign some value in here then it's not going to give you the error now let's try to declare a variable with the underscore for example underscore my well is equal to 1 and press Enter it's totally valid and now let's try to declare a variable with a dollar symbol so dollar and my well and this is equal to 6 for example and it's also totally valid okay so this console will help you to debug your code and even show you if you have some errors in your code right now let's see how these variable works in JavaScript so for example you declare a variable called X is equal to 6 and Y is equal to 10 okay so I have already told you that you can do something like X plus y and it will give you the sum and whenever you do X or Y minus X then it will give you the subtraction you can do the opposite X minus y and it will give you the minus value right now to multiply these two numbers you can do Y multiplied by X so Astrix is the multiplication symbol in JavaScript so press Enter it will give you the multiplication whenever you do for example Y divided by X so Y divided by X is 1.6 whatever is the value of this division it will give you now there is a special symbol called mod in JavaScript so whenever you do Y mod X and press Enter then it will give you the remainder of the division of the variable you are dividing so the value of y was 10 and the value of x was 6 so when you divide Y by X then you will get 4 as remainder so this modulus gives you the remainder now javascript is also intelligent enough to recognize what operation you want to do with your numbers for example you want to do 4 minus 4 multiplied by 5 it will give you minus 16 and not 0 right so it will give the importance to the multiplication first and then the subtraction or for example you want to divide and multiply and add or subtract something at the same time it will give the precedence something like multiplication then division then addition and then the subtraction ok or if you want to make your code more clear you can do something like this so you can add a bracket here and inside this bracket you can do 4 minus 4 and then after the bracket you can do multiply it by 5 and press Enter it will give you 0 because this bracket has more priority than your multiplication or your division symbols okay so in this way you can use these variables and comments in JavaScript by the way if you are using Firefox you can also use these consoles so go to Firefox and right-click and once again go to inspect element and in here go to console and you have this console here X is equal to 1 it will give you the same answer okay so if you have a Firefox or Google Chrome you can debug your code and find errors in it and do many more things with this view here so I hope you've enjoyed this blog
0 Comments