Points we covered in this tutorial:-
- Multi line comments – Multi line comments start with /* and end with */. It is represented by the forward slash (/) and a (*) asterisk sign.
As we already know that variables are known as containers which use to store data and values. Variables allow you to provide a particular name to your container which helps to manipulate and change your data. So there are also some predefined rules for declaring the variable name.
The general rules for constructing names for variables (unique identifiers) are:
- Names can contain letters, digits, underscores, and dollar signs.
- Names must begin with a letter.
- Names can also begin with $ and _ sign.
- Names are case sensitive (y and Y are different variables).
- Automatically declaration
- Using Var Keyword
- Let keyword
- Const Keyword
Example Using Var
<!DOCTYPE html> <html> <body> <p>This is an example to declare variable using the var keyword.</p> <p id="add"></p> <script> var x = 5; var y = 6; var z = x + y; document.getElementById("add").innerHTML = "The value of z is: " + z; </script> </body> </html>
This is an example to declare variable using the var keyword. The value of z is: 11
Example Using Let
<!DOCTYPE html> <html> <body> <p>In this example, x, y, and z are variables.</p> <p id="add"></p> <script> let x = 5; let y = 6; let z = x + y; document.getElementById("add").innerHTML = "The value of z is: " + z; </script> </body> </html>
In this example, x, y, and z are variables. The value of z is: 11
Example Using Const
<!DOCTYPE html> <html> <body> <p>In this example, x, y, and z are variables.</p> <p id="demo"></p> <script> const x = 8; const y = 12; const z = x + y; document.getElementById("demo").innerHTML = "The value of z is: " + z; </script> </body> </html>
In this example, x, y, and z are variables. The value of z is: 20
Example of innerHTML
To set the
innerHTML of an element, you first need to select the element using one of the various methods available (e.g.,
querySelector, etc.), and then assign the desired HTML content to the
Example of document.write()
It is commonly used for simple testing and demonstration purposes but should be used with caution in real-world applications, as it has some limitations and can lead to unexpected behavior.
<!DOCTYPE html> <html> <body> <p>Input Using document.write().</p> <p>Never call document.write after the document has finished loading. It will overwrite the whole document.</p> <script> document.write(7 + 6); </script> </body> </html>
Input Using document.write(). Never call document.write after the document has finished loading. It will overwrite the whole document. 13
Example of window.alert()
It is a method that displays a simple dialog box, commonly known as an alert box, with a specified message and an “OK” button. It is often used to show a message or prompt the user for some information. When the alert box is displayed, it temporarily interrupts the user’s interaction with the web page until the user clicks the “OK” button or closes the alert box.
<!DOCTYPE html> <html> <body> <p>The process of alert function.</p> <script> window.alert("Welcome to APalgorithm"); </script> </body> </html>
Example of console.log()
It is a standard way to produce output is by using the
console.log() function. This function allows you to display text or values in the console, which is often the developer’s primary tool for debugging and logging information.
Hey there, I’m Anshul Pal, a tech blogger and Computer Science graduate. I’m passionate about exploring tech-related topics and sharing the knowledge I’ve acquired. With two years of industry expertise in blogging and content writing, I’m also the co-founder of HVM Smart Solution. Thanks for reading my blog – Happy Learning!