Below are the reserved keywords in the JavaScript which can not be used as ‘variable’ and ‘function’ names etc.
Keywords | |||
---|---|---|---|
abstract | boolean | break | byte |
case | catch | char | class |
const | continue | debugger | default |
delete | do | double | else |
enum | export | extends | false |
final | float | for | function |
goto | if | implements | import |
in | int | interface | long |
native | new | null | package |
private | protected | public | return |
short | static | super | switch |
this | throw | throws | transient |
true | try | typeof | var |
void | volatile | while | with |
JavaScript has three types of data.
Note: All the numbers are considered as ‘floating point’.
Variables can be define using keyword ‘var’. Further, JavaScript is untyped language i.e. a variable can hold any type of value.
In the below HTML, ‘p’ tag with id ‘p_name’ is defined at Line 10. This id will be used to write some text using JavaScript,.
1 <!-- js.html -->
2
3 <!DOCTYPE html>
4 <html>
5 <head>
6 <title>JavaScript</title>
7 </head>
8 <body>
9
10 <p id='p_name'></p>
11
12 <!-- import JavaScript files here -->
13 <script src="asset/js/my_javascript.js"></script>
14
15 </body>
16 </html>
Two variables are defined at Lines 9-10 of type ‘string’ and ‘float’ respectively. Then ‘getElementById’ is used to locate the tag with id ‘p_name’ and the text is inserted as shown in Line 11. Lastly, the ‘+’ sign is used to concatenate the values as Line 11.
1 // asset/js/my_javascript
2
3
4 // print message on the webpage
5 document.write("Hello World from JavaScript!<br>");
6
7
8 // variable example
9 var your_name = "James";
10 var age = 20;
11 document.getElementById("p_name").innerHTML= "Hello "+ your_name + "<br>Age : " + age;
Hello James
Age : 20
Hello World from JavaScript!
Warning: If we import the ‘JavaScript’ file between the ‘ending-head-tag’ and ‘start-body-tag’, then Message ‘Hello Meher . . . ’ will not be displayed as ‘JavaScript’ will execute before the loading of the page; and JavaScript can not find the id ‘p_name’.
Note: All the JavaScript/HTML codes will be added below the existing codes, therefore only the newly added code will be shown in the rest of the tutorial.
‘prompt’ can be used to read the values from the user
1 // prompt
2
3
4 var x = prompt("enter a number");
5 document.write("2 * ", x, " = ", 2*x + "<br>");
A pop-up window will appear due to prompt. If we provide the input value as 3, then below output will be generated.
3 * 3 = 6
Note: The ‘,’ and ‘+’ can be used to combine various outputs as shown in above example
Various operators are shown in this section.
Example: ‘( (a > b) ? a/b : b/a) )’ i.e if ‘a>b’ then do ‘a/b’ else do ‘b/a’
‘Number’ is used to convert the string into numbers.
1 // string to number conversion
2
3 document.write("2 + Number('3.4') = ", 2 + Number('3.4'), "<br>");
Below is the output of above code.
2 + Number('3.4') = 5.4
A string or float value can be converted into integer using ‘parseInt’.
1 // int conversion
2
3 document.write("2 + parseInt('3.4') = ", 2 + parseInt('3.4'), "<br>");// string to int
4
5 document.write("2 + parseInt(3.4) = ", 2 + parseInt(3.4), "<br>"); // float to int
Below is the output of above code.
2 + parseInt('3.4') = 5
2 + parseInt(3.4) = 5
The ‘parseFloat’ or ‘Number’ can be used to convert the value into float values.
1 document.write("2 + parseFloat('3.4') = ", 2 + parseFloat("3.4"), "<br>"); // parseFloat
Math’ library contains various useful function as shown below.
1 // math
2 document.write("pi = ", Math.PI, "<br>");
3 document.write("e = ", Math.E, "<br>");
4 document.write("similarly we can use 'abs', 'floor', 'ceil' and 'round' etc. <br>");
5 document.write("random number : ", Math.ceil(Math.random()*20), "<br>"); // enter random number
Below is the output of above code.
pi = 3.141592653589793
e = 2.718281828459045
similarly we can use 'abs', 'floor', 'ceil' and 'round' etc.
random number : 16
Below are the some of the useful ‘string-styles’.
1 // string
2 document.write("olusola".toUpperCase(), "<br>") // uppercase
3 w = "Olusola"
4 document.write(w.toLowerCase(), "<br>") // lowercase
5 document.write(w.small(), "<br>") // small
6 document.write(w.bold(), "<br>") // bold
7 document.write(w.strike(), "<br>") // strike
8 document.write(w.fontsize("5em"), "<br>") // strike
9 document.write(w.link("http://folklight.ng"), "<br>") // link
10 document.write(w.fontcolor("red").fontsize("12em"), "<br>") // multiple
The image below shows the output of above code.
In JavaScript, the arrays can store different types of values as shown in below code.
1 // arrays
2 arr = [15, 30, "Olusola"]
3 for(a in arr) document.write(arr[a], " ");
4 document.write("<br>");
5 document.write(arr.pop(), "<br>"); // remove last element
6 arr.push("Emeka"); // add element to end
7 document.write(arr.pop(), "<br>");
8 document.write("lenght of array: ", arr.length, "<br>");
15 30 Olusola
Olusola
Emeka
lenght of array: 2
© Copyright 2025 | FolkLight Studios