Javascript Basics tutorial


 

 

Learn JavaScript

 

 Javascript Table of Contents

  1. Introduction to JavaScript.
  2. Data types in JavaScript
  3. Types of operators in JavaScript:
  4. Getting started with JavaScript
  5. Arithmetic Operators
  6. Conditional  Statements
  7. Looping Statements
  8. How to check errors in JavaScript

 

 

 

Introduction to JavaScript.

JavaScript is a one of the scripting languages. Used to perform client-side calculations and validations on form data.
 

Basically, there are 2 types of JavaScript.

  1. Client side:  To perform validations, some calculations at client side.
  2. Server side (SSJS) : To perform some manipulations on the server-side, and allowing  applications to communicate with a database. etc.,
    Ex:Node.

Data types in JavaScript


JavaScript is a loosely typed language. It means it can store  different types of data each time. The type is dynamic. Its type is decided, the value that it is storing.

Variable: A variable is a memory location name and used to store a small piece of data for later use. Variable starts with var keyword, but it is optional.
 

TOC ↑

 

example:


var number=5

var customer_name="John";  

var age= 33;

var city= "Delhi";

 

Types of operators in JavaScript:

  • arithmetic: [+, -, *, /, %]
  • relational: [>, <, >=, <=, !=, ==]
  • logical:[&&, ||, !]
  • increment: [++]
  • decrement: [--]

JavaScript Conditional Statements 

Conditional statements generally used in decision-making. By comparing two values, it chooses the block /statement to execute.

  • if 
  • if..else
  • if....elseif..else

JavaScript Looping Statements


Loop : Looping statements are used to repeat a certain number of lines until a given condition is true. In JavaScript there  are 4 types of loops available.

  • while
  • do-while
  • for
  • for/in

Top ↑

Getting started with JavaScript

To start with JavaScript, no coding knowledge required. If you know HTML, that will make things easier.

JavaScript's code will be written inside the script tags <script>  ... </script> in the HTML page. 

Every browser contains a JIT(Just In Time). It is an inbuilt compiler, to compile(check for errors) JavaScript.

Let's see how to run the first JavaScript program...


example: 

 Hello.html


<html>

 <body>

 

<script type="text/javascript">

   

 document.write("Hello World!");

 

</script>  
</body>   
</html>

 Top ↑

1. Create a folder practice

2. Create a file called Hello.html using any editor of your choice. Even windows notepad also works, and save it in practice folder.

Here I am using sublime text editor. You can download it, from this link.

Now your code looks as given below picture: 



Next, open program in browser.

Then you will see the result:
 
 
 
 
e-trainings-js-hello-demo

 

UP ↑

 

Formatting Text using HTML in JavaScript

 

Using HTML tags, text can be formatted. The below program demonstrates how to use HTML tags in JavaScript.

 
example2:  

 Hello.html

<html>  
<body> 
<script type = "text/javascript"> 
document.write("<h1>Hello World!</h1>"); 
</script>  
</body>
</html>
 
output
 
 
e-trainings-js-html-tag-demo

 
 
 
 

Top ↑

 

Comments in JavaScript:

Commented line are ignored by compiler.  

To comment single line you use:  // (double forward slash) as I added in the above program

To comment multi lines you use: /* ... */
 
 
example: 
 
<html> 
 <body>
 <script type="text/javascript">  
 
  document.write("Welcome to JavaScript"); 
  document.write("<br />JavaScript is Fun "); // this text printed in new line
 
</script>  
</body>   
</html>


 

Arithmetic Operators

example: 
 
<html>
<body>
<script type="text/javascript">

 

num1=5
num2=6
 
addition = num1 + num2;
subtraction = num1 - num2;
multiply = num1 * num2;
division = num1 / num2;
remainder = num1 % 2;

 

document.write("<br>Number1 = " + num1); 
document.write("<br>Number2 = " + num2);

document.write("<br>Addition = " + addition ); 

document.write("<br>Subtraction = " + subtraction );

document.write("<br>Multiply = " + multiply );

document.write("<br>Division = " + division );

document.write("<br>Remainder = " + remainder);


</script>
</body>
</html>

output

 

e-trainings-js-arithmetics-demo

 

Goto Top ↑

 

 
Javascript Conditional  Statements

Using if statement in JS

examples:

<html>
<body>
<script type="text/javascript">

 

a = 15

b = 16;

document.write("<br>");

if(a > b)

{      

    document.write(a+" is greater"); 

}  

else 

{     

     document.write(b+" is greater");

 }

</script>
</body>
</html>

 output

 

e-trainings-js-if-else-demo

 

Top ↑

if..else-if  in JS

 

<html>
<body>
<script type="text/javascript">

 

score = 85

 

document.write("<br>");


if(score >= 80 && score <= 100)

{      

    document.write(" Grade A"); 

}  

else if(score >= 60 && score < 800)

{      

    document.write(" Grade B"); 

}

 else 

{     

     document.write(" Grade C");

 }

 
</script>
</body>
</html>

 output

 

 

e-trainings-js-if-elseif-else-demo

 

Top ↑

Javascript Looping Statements
 

While Loop In JS

 
While takes condition and repeats a block of statements until the condition is true. 
Once the condition is true, it stops repeating and comes out of the loop.
 
syntax:
  
while(condition)
{
statement1;
statement2;
... 
statement n;
}
 
 
 
 
 example:
 
 
<html>
<body>
<script type="text/javascript">
i=0;
while(i <= 5)
{
document.write(i);
document.write("<br>");
i++;
}
</script>
</body>
</html>


 output

 

e-trainings-js-while-demo

 

Top ↑

Do-while in JS

 
It is similar to while. The difference is, It runs the loop first, and then the condition is checked.

 
 
syntax:  
 
do
{
statement1;
statement2;... 
statement n;
} 
 while(condition);
 
 
 
 
 
 
example: 
 
<html>
<body>
<script type="text/javascript">
i=10;  
do{  
document.write(i);  
document.write("<br />"); 
i--; 
} while(i>=0);
 
</script>
</body>
</html>

  output

 

e-trainings-js-do-while-demo

 

 

Top ↑

For Loop in JS

For-Loop, basically used with arrays. For statement divided into 3 parts 

initialization, condition, incrementation.

 
 
syntax:
 
for (initialization;condition;incrementation) 
{
statement1;
statement2;... 
statement n;
} 
 
 
example:
 
<html>
<body>
<script type="text/javascript">

for (i=0;i <= 5;i++)
{
document.write(i);
document.write("<br />");
}
</script>
</body>
</html>

  output

 

e-trainings-js-for-demo

 

 

 

Top ↑

For/in Loop in JS


For..In is another loop in JavaScript used with array objects.

example:
 
<html>
<body>  
<script type="text/javascript">
 
var customer={firstName:"John", lastName:"Doe", age:25, city : "NY"};  
var key;
 
for (key in customer) { 
document.write(person[key] );
document.write("<br />"); 
}
 
</script> 
</body> 
</html>

output

 

e-trainings-js-for_in

 
 

Move Top ↑

with keyword

 

With is a kind of block. It takes any object as parameter, and all the

methods available in that object are accessible to that block. In the following example, write

method is used without specifying  object name(document) before it.

 
example:
 
<html>  
<body>  
<script type="text/javascript">
  
with (document) { 
write("<br> Hello"); 
write("<br> Hi there"); 

}

</script> 
</body> 
</html>

 

 

output

 

 

 

e-trainings-js-with_example2

 

Go Top ↑

 

example2:

 

<html>  
<body>  
<script type="text/javascript">
 with (Math) {  
document.write(sqrt(16)); 
}  

</script>
</body>
</html>

 

 output

 

e-trainings-js-with_example2
 
 
 

 How to check errors in JavaScript


 If there is an error JavaScript doesn't show result. To rectify syntactical errors,
 
In Chrome
  • right click on browser window and select inspect.
  • Next, select console tab for error details

 
coding-zon-js-error-check

 
 To know how to check  errors in other browsers  visit the link

 

 

JS Functions

 

 

 

 

 Was this tutorial helpful? Please comment below...

 

 

 

 

Top ↑

 


Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP