You can access the HTML elements using ids, classes, etc. but you can also access them using objects. In Document Object Model (DOM), the HTML page is described as a tree structure in which every node represents an object.
1. Your First Program: ‘Hello, World!’
You can write using the dot syntax, in which first you have to write the object name and then a method or property. We’ll now use the document object which represents your HTML page. You have to use the write method to display your text on the HTML page as follows:
Type it in between the script tags and save your HTML page with .html extension. When you open the page in the browser, these words will be printed on the screen.
Lets say we have five subjects and we want to evaluate the percentage of the marks of students.
In the first line of code
prompt(‘question’, ‘default value’)
Next, we equate it to the var marks so that the new value that the user enters will now get assigned to this variable.
marks = prompt(‘Enter your marks ’, ‘’)
Default value is usually left blank but if there is no user input, then the default value can be used instead. Next, we assign a new variable result for the output.
var result = marks * 0.002
Now that you have got the user input, you can use alert function to display the result:
alert('result: ' + result)
We use + operator to add the value of result we got as output.