Introduction
Variables
- var
Using var keyword we can declare local and global variables. e.g. var name='JavaScript '; - thisUsing this keyword we can declare variables like this.name='JavaScript '. This keyword is always declared a global variable.
- letLet the keyword is used for declaring a block scope variable.
- constAs the name suggests, using const keyword we can create any read-only variable.
A variable declared using the var or let statement with no value will automatically be assigned with default value undefined.
If we want to access any variable without prior declaration will throw Reference error exception.
You can put comments into JavaScript code, just like you can in CSS:
Example
Comments
- <html>
- <body>
- <script language="javascript" type="text/javascript">
- /*
- Comment here
- */
- </script>
- </body>
- </html>
- <html>
- <body>
- <script language="javascript" type="text/javascript">
- //This is Comment
- </script>
- </body>
- </html>
Setting Up Code
- <html>
- <body>
- <script language="javascript" type="text/javascript">
- /* Description:-
- Here "document" is the object and
- "write" is the method of this object.This method write what ever message you pass into this as parameter.
- */
- document.write("Hello World!");
- </script>
- </body>
- </html>
When this code executes then the following result is displayed,
- document.body: Sets or gets the document's body (the <body> element) or null if no such element exists.Example
- <html>
- <body>
- <script language="javascript" type="text/javascript">
- //if want to get Id of body tag then you can use document.body.id.
- document.body.style.backgroundColor = "yellow";
- </script>
- </body>
- </html>
In the above example document. body. style.backgroundColor="yellow" replaces <body> tage background color with specified color. - document.cookie: Cookie property sets or gets all name/value pairs of cookies in the current document.
Example
- <script language="javascript" type="text/javascript">
- //declare x variable and assign it with document.cookie value;.
- var x = document.cookie;
- //used for popup.It will popup of x value
- alert(x);
- </script>
- document.getElementsByTagName(): This function return all element of given HTML tage.Example
- <script language="javascript" type="text/javascript">
- var h1 = document.getElementsByTagName("h1")[0]; // Get the first <h1> element in the document
- </script>
- document.getElementById(): This is the most often used JavaScript method. It is useful when you want to get an element from your HTML on your document. It will return null when no element found with specified Id.
- <html>
- <body>
- <p id="demo">Value will replace from Hello World.</p>
- <script language="javascript" type="text/javascript">
- //Here demo is element Id which we want to get and innerHTML property gets or sets the HTML content (inner HTML) of an element.
- document.getElementById("demo").innerHTML = "Hello World";
- </script>
- </body>
- </html>
- document.getElementsByClassName(): This method returns the list of all HTML elements in the document with the specified class name, which can be accessed using the index numbers. The Index number starts at 0.
- <html>
- <body>
- <div class="bla">First element.</div>
- <div class="bla">Second element.</div>
- <script language="javascript" type="text/javascript">
- //Here x will get collection of all html element which have class name bla.
- var x = document.getElementsByClassName("bla");
- //set then value of first element as Hello World!.
- x[0].innerHTML = "Hello World!";
- </script>
- </body>
- </html>
- document.getElementsByName(): This method works exactly the same as document.getElementsByClassName() only difference is document.getElementsByClassName() needs to pass class name a argument and in document.getElementsByName() needs to pass element name as parameter.
- <html>
- <body>
- <div class="bla">First element.</div>
- <div class="bla">Second element.</div>
- <script language="javascript" type="text/javascript">
- //Here x will get collection of all html element which have classname bla.
- var x = document.getElementsByClassName("bla");
- //set then value of first element as Hello World!.
- x[0].innerHTML = "Hello World!";
- </script>
- </body>
- </html>
- document.createAttribute(): This function is used to create an HTML attribute. e.g.
- <script language="javascript" type="text/javascript">
- var h1 = document.getElementsByTagName("H1")[0]; // Get the first <h1> element in the document
- var att = document.createAttribute("class"); // Create a "class" attribute
- att.value = "democlass"; // Set the value of the class attribute
- h1.setAttributeNode(att); // Add the class attribute to <h1>
- </script>
In this article we learned about the basics of JavaScript. In upcoming articles we will learn about methods, events, etc. This is my first article, so your comment, critics and suggestions are welcomed.
Read more articles on JavaScript

SubashPosted Aug 30, 2016, 12:38 AM
Useful one thank you
Ravi PatelPosted May 18, 2016, 3:37 AM
nice explanation
Vignesh ManiPosted Apr 3, 2016, 5:30 PM
Nice one.
Kuppurasu NagarajPosted Apr 3, 2016, 12:06 PM
nice
shaikh abrarPosted Mar 30, 2016, 1:17 AM
Nice
Yogendra YadavPosted Mar 21, 2016, 8:08 AM
nice one
Pradeep SahooPosted Mar 18, 2016, 8:39 PM
good description ....
Imtiyaz AnsariPosted Mar 16, 2016, 2:50 PM
Nice Describe for Beginners
Amit Kumar SinghPosted Mar 16, 2016, 6:42 AM
Nice one
Debasis SahaPosted Mar 15, 2016, 1:52 PM
Good One..
Saillesh PawarPosted Mar 15, 2016, 8:44 AM
Please elaborate let: Let keyword is used for declaring a block scope variable.
Saillesh PawarPosted Mar 15, 2016, 8:42 AM
Nice Share.
Mohammed IbrahimPosted Mar 6, 2016, 9:57 AM
nice
Kirtan ParmarPosted Mar 6, 2016, 1:37 AM
its really helpful for beginners
Ankit BansalPosted Mar 6, 2016, 12:59 AM
Nice share..Looking forward for a good and more expressive series of JS..JavaScript Rocks..
KeyurPosted Mar 6, 2016, 12:45 AM
Nice...
Humayun Kabir MamunPosted Mar 5, 2016, 10:51 PM
Nice...
Asfend YarPosted Mar 5, 2016, 1:56 PM
nice
Sonu ChaudharyPosted Mar 5, 2016, 12:12 PM
Plz explain how to print value by JavaScript
Vignesh ManiPosted Mar 5, 2016, 9:08 AM
Nice
Satyam LimbaniPosted Mar 5, 2016, 8:24 AM
informative article for beginner
Amit Kumar SinghPosted Mar 5, 2016, 6:46 AM
Nice One