WebTrainingRoom Online Courses
Learn JavaScript, Free JavaScript Tutorial for Beginners, Interactive JavaScript Examples

JavaScript Programming Tutorial

How to Write Javascript Code

Javascript tutorial for beginners
Learn basic JavaScript Syntax, how to write javascript code, On page JavaScript, how to write External javascript file, add javascript file reference.

In this JavaScript Tutorial your learn basic JavaScript syntax, how to write javascript in different ways. Before we write any javascript code, you must know that javaScript is case sensitive .

We can write javascript in two way

  • On page javascript
  • External Javascript file (.js )

On page JavaScript syntax

Javascript code block always starts with <script> and ends with </script>

<script>
var _welcomeMessage="Welcome to WebTrainingRoom";
window.alert(_welcomeMessage);
</script>

In above example, we declared a variable called _welcomeMessage, then assigned a string value within "".

  • To declare a variable use var then name of the variable
  • Once you assign a value to that variable, that become the data type of variable, in our case above variable become string type after value assigned
  • You cannot change the data type of any variable within the program scope, suppose you have written var x=10;, now within the program you cannot write var x="Hello World";
  • Any statement in javascript ends with ; semicolon
  • JavaScript is case sensitive, so var x and var X are two different variables

External Javascript file (.js) syntax

You can create a separate javascript file and call that file from your webpage. Here are few characteristic of external javascript file.

  • External javascript file has to be saved with .js extention
  • when you create a js file, you can not write any other type of code in that file, everything has to be javascript only
  • To access the javascript function in your webpage, add file reference this way <script src="~/scripts/yourJsFilename.js"> </script>

Learn JavaScript Step by Step


Comment
Name Email Website
Subscribe
 
JavaScript Syntax

Learn JavaScript Online

Consulting