JavaScript Tutorial : Introduction for
beginners (Basic)

&& (AND)
|| (OR)
! (NOT)

1. <script type=”text/javascript”>
2. var No1 = Number(100);
3. var No2 = Number(25);
4. document.writeln(No1 > 120 && No2 < 30);
5. document.writeln(No1 < 120 || No2 < 30);
6. document.writeln(!(No1 > No2));
7. </script>

(!=) — not equal to
(==) — equal

(>) — greater than
(<) — less than
(>=) — greater than or equal to
(<=) — less than or equal to

1. <script type=”text/javascript”>
2. var No1 = new Number(101);
3. var No2 = new Number(20);
4. document.writeln(No1 != No2);
5. document.writeln(No1 == No2);
6. document.writeln(No1 > No2);
7. document.writeln(No1 < No2);
8. document.writeln(No1 >= No2);
9. document.writeln(No1 <= No2);
10. </script>

5.5) Increment and decrements

(++) — Increment
(–) — Decrement

1. <script type=”text/javascript”>
2. var No1 = new Number(101);
3. var No2 = new Number(20);
4. document.writeln(++No1); // No1 + 1
5. document.writeln(++ No2); //No2–1
6. </script>

6) Order of operators

() — Brackets
(++, –) — Increments and Decrements
(*, /, %) — Multiplication, Divided, Mod
(+, -) — Addition and Subtraction
(>, <, >=, <=) — greater, less, greater or equal, less or equal
(==, !=) — equal, not equal
(!) — NOT
(&&) — AND
(||) — OR

1. <script type=”text/javascript”>
2. var No1 = new Number(101);
3. var No2 = new Number(20);
4. document.write((No1+No2)/2*5);
5. </script>

7) Special Symbol

(\’) — to show single quotation (‘) mark
(\”) — to show double quotation (“) mark
(\\) — to show backslash (\) mark

1. <script type=”text/javascript”>
2. var txt = “\”CtechF\” — \\2019\\, \’Technology\’”;
3. document.write(txt);
4. </script>

8) Comments

1. <script type=”text/javascript”>
2. // Welcome to my Website
3. document.write(“<h3>ctechf.com</h3>”);
4. //document.write(“<p>Hello World!</p>”);
5. </script>

1. <script type=”text/javascript”>
2. /*
3. Welcome to my Website
4. document.write(“<p>Hello World!</p>”);
5. */
6. document.write(“<h3> Welcome to my Website</h3>”);
7. </script>

9) Conditional Statement

1. if (condition){
2. // statements
3. }

1. <script type=”text/javascript”>
2. var date = new Date();
3. var time = date.getHours();
4. document.writeln(“<h2>” +time+ “</h2>”);
5. if (time > 18) {
6. document.writeln(“<h5>Good Night</h5>”);
7. }
8. </script>

1. if (condition){
2. // statements
3. } else {
4. // statements
5. }

1. <script type=”text/javascript”>
2. var date = new Date();
3. var time = date.getHours()
4. document.writeln(“<h2>” +time+ “</h2>”);
5. if (time < 12) {
6. document.writeln(“<h5>Good Morning</h5>”);
7. }else {
8. document.writeln(“<h5>Good Day</h5>”);
9. }
10. </script>

1. if (condition){
2. // statements
3. } else if (condition) {
4. // statements
5. } else if (condition) {
6. // statements
7. } else {
8. // statements
9. }

1. <script type=”text/javascript”>
2. var Marks = Number(98);
3. document.writeln(“<h2>” +Marks+ “</h2>”)
4. if (Marks < 36) {
5. document.writeln(“<h5>WEAK</h5>”);
6. }else if (Marks < 56) {
7. document.writeln(“<h5>GOOD</h5>”);
8. }else if (Marks < 66) {
9. document.writeln(“<h5>VERY GOOD</h5>”);
10. }else {
11. document.writeln(“<h5>EXCELLENT</h5>”);
12. }
13. </script>

1. switch (condition){
2. case “value”:
3. //Statement
4. break;
5. case “value”:
6. //Statement
7. break;
8. case “value”:
9. //Statement
10. break;
11. .
12. .
13. .
14. .
15. default:
16. //statement
17. break;
18. }

10) Loop

1. for (//) {
2. //Statement
3. }

1. <script type=”text/javascript”>
2. var a = Number(0);
3. for (a=0; a<=5; a++) {
4. document.write(“This number is “ +a);
5. document.write(“<br>”);
6. }
7. </script>

1. do {
2. //Statement
3. } while(condition)

1. <script type=”text/javascript”>
2. var a = Number(1);
3.
4. do {
5. a = a + 1;
6. } while (a <= 10);
7. document.write(a);
8. </script>

1. while (condition){
2. //Statement
3. }

1. <script type=”text/javascript”>
2. var a = Number(1);
3.
4. while (a <= 8) {
5. a = a + 1;
6. }
7. document.write(a);
8. </script>

Hi Friends,

Hope this post will help you to Learn something, If you enjoy my work then please share my posts
with your buddies and anyone who might be interested in programming and don’t forget to follow.

Undergratuate in University of Moratuwa. Faculty of Information Technology