INTRODUCTION TO JAVA SCRIPT.
Comments in Java script.
(00:00)
Hello guys, welcome to our series Java script. In our last segment we saw how we can run Java script in different ways. We saw through head tag, body tag, if we add external .js file in our current HTML program and run it that what are it’s advantages and disadvantages, we studied all these in our last topic.
If you have any questions or query related to last topic then you can contact us on forums.learnvern.com we will revert back with solutions for your questions and queries.
Our today’s topic is How to add comments in java script? Basically we know comments are very important part. Whatever we are going to study in our whole course of Java script is very useful in Large Application Domain.
Means if we have created a large website or we have created an application which runs on basis of java script, we mostly work through functions whenever we work with any application or huge website. For example if I have created a function of my one form validation ten how will I know that the function has been created for form’s validation. If I have created another function in same way for some other form’s validation.
If in my website there are more than one forms, then I need to validate all my forms. Then how will I know what is the work of my each function? We can’t give long names to each and every function. We usually use limited character or alphabets only. To identify those we use comments in java script.
There are other advantages also of adding comments, comments are not visible to the user but fir some programmer who is doing the programming. Let’s say if I work in some company and have created a website and I have added functions to it. So if I leave that job and join some other company, and some developer is hired in my place to work on the same website, so even he should have the idea that what is the purpose of these functions.
Comments are also very useful to pass information, if we are creating a huge application, then sometimes it happens that if I created a function 2-3 months earlier in some application and then I started working on some other application. Then it is not necessary that I will remember why I have written each and every code in that application.
In order to remember that, comments are very useful. Comments are very useful to memorize for us also that why we have written a particular function and even for other programmers that so and so function has been written for which purpose.
Moving further, as it is written here, the java script comments are meaningful way to deliver message. Means for delivering message, as I said if I leave the job and another employee comes, so it will be helpful information for him that so and so task has been created for which purpose, I will be delivering message to unknown person that because of this particular task, this code has been written.
It is used to add information about the code, warnings or suggestions, so that end user can easily interpret the code. Means, the information about the code comes but along with that if we have to pass any warning related to task, we can do that also or we can also pass suggestions to the end user so that he can easily interpret it.
(04:12)
Last line says whenever we run Java script’s code, whatever we have added through comment section get ignored only executable code is run through our browser. So we need to remember that comments can never be executed.
Now the comments which we are adding have some advantages also, why do we add comments? Mainly it has two advantages of java script. First is it makes code easy to understand, it can be used to elaborate the code so that end user can easily understand the code. We use comment in order to make the code understandable for other users.
Second is to avoid unnecessary code, it can also be used to avoid the code being executed. For example, if I have written some code, now I want this code to be written in another optimized way. If my current code is 10 lines long but now I have another better way through which I can write my code in 4-5 lines instead of 10 lines.
Now I won’t delete my 10 line code until I find a way to make it 4 line code, so till then I will comment y first code, means until and unless I find another way I will keep my first code written. If I am successful in finding another way to write my code then I can remove my first 10 line code, it becomes unnecessary or me. But if I am unsuccessful then my first code is still there, I can uncomment it and use it.
It means if you write a code but you don’t want to execute it, so I can write it as comment. Maybe it will be useful in future to you or someone else. Comments are useful in this way also. Not only delivering messages or passing information, but also writing code and not executing it in that case also comments are helpful. Now if you write code which can be helpful later in future and you comment it, it helps in performance of our application or website. How does that happen?
Because our code gets clean and it gets reduced for execution. Because commented code cannot be executed. Other things get executed and performance is improved. So this is another advantage of comment.
There are two ways available in java script, single line comments and multiple line comments in which you can add your comments. So if I am adding in single line, then I can add my comments with the help of double slash (//).
When we conducted HTML course, that time also we saw that we added single line comments with double slash and there is another way also which we will learn later. Through these two ways either single lie or multiple line we can add comments.
Or that we will open sublime text first, here HTML, example of single line comments and save under file name single line comments. So this is our HTML program, now we have three ways, we can add in head or body or external js file. Well external we will do if we need it during our course.
When the need appears I will inform you why we are adding external .js file. Currently I am adding script in body tag. Script, let’s say I add document. write ( “this is LearnVern”); save and we will run this program. So this is our program running. You can see here in program whatever I have written has appeared here, but now I have to indicate other users that this line is written about LearnVern.
(09:02)
Then how will I write this I single line? Double slash // and now will add comment. Below line indicates about LearnVern and save. After saving it if I reload my page now, commented section does not load here, only the required portion in html is shown here. So this was our single line comments. How we add comments related to single line.
If I copy the same program and generate another program but instead of single I will write multi line comments. So here for multi line comments first I will save under the name multi line comments, okay, so this was our multi line comments now here also you can see only ‘This is LearnVern’ So this we can add in our earlier program also, one more document.write(“ In this program single line comment is added”); so if we move onto previous program there you can see it is written This is LearnVern, now here we have to add new line to separate tis we will add exclamation mark and will add some space in next line which will be visible in our program.
Similarly, this document.write we will take it here and we can add in this program multi line comment is added. To add multi line comments what do we use? We use /**/ so whatever content is written between these two comes under multi line comments. Like, /* below lines explains about learnvern*/ and save and now I will reload my first program. The portion which was not added is now added but our comments are still not visible.
So what does that indicate? Whether you have added single line comment or multi line comment, it will never be shown in the body part of our browser or document. We can see in both the programs, both are different programs but comment are not visible. But we have added comment in our program, one is via single line i.e. through // and in multiple line through/**/ you can add as much as comment you want.
This was our first single line comments // and this is our multiline comments /**/, so this was our way through which we can add single line and multiline comments in java script. We also saw that why comments are used in java script. The two methods of comment which we discussed today is not limited to Java script only, it is common for mostly all programming languages. So guys in today’s section if you have any questions or queries then contact us on forum.learnvern.com we will resolve all your issues.
In our next segment we will see how to add variables in java script? What all variables are available? We will study all these in next topic.
Thank you.
( video duration- 13 mins 5 seconds).
Share a personalized message with your friends.