Javascript Tutorial 1

JavaScript is a very powerful language. It can be used both within any browser in the world. On top of that, it can be used to write server-side code using node.js.

Print data/values

To print out any data or results..you can use document.write and console.log

document.write:

you can use JavaScript to write directly to the HTML document

<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>

<p>My first paragraph.</p>

<script>
document.write(Date());
</script>


</body>
</html>

Result:

My First Web Page

My first paragraph.

Mon May 26 2014 13:23:25 GMT+0530 (India Standard Time)

console.log

If your browser support debugging, you can use the console.log() method to display JavaScript values in the browser.

Activate debugging in your browser with F12, and select “Console” in the debugger menu.

<!DOCTYPE html>
<html>
<body>

<h1>My First Web Page</h1>

<script>
a = 5;
b = 6;
c = a + b;
console.log(c);
</script>


</body>
</html>

Result:

Activate debugging in your browser (Chrome, IE, Firefox) with F12, and select “Console” in the debugger menu.

 

Variables & Types

every variable is defined using the var keyword, and can contain all types of variables.

We can define several types of variables to use in our code:

var myNumber = 3;                   // a number
var myString = "Hello, World!"      // a string
var myBoolean = true;               // a boolean

There are two more advanced types in JavaScript. An array, and an object. We will get to them in more advanced tutorials.


var myArray = [];                    // an array
var myObject = {};                  // an object

Atop of that, there are two special types called undefined and null.


When a variable is used without first defining a value for it, it is equal to undefined. For example:


var newVariable;
console.log(newVariable); //prints undefined

Eg:

<!DOCTYPE html>
<html>
<body>
<script>
var myNumber =4;
var myString = “variables are great”;
var myBoolean = true;
console.log(“mynumber is equal to:”+ myNumber);
console.log(“myString is equal to:”+ myString);
console.log(“mybool is equal to:”+ myBoolean);
document.write(“mynumber is equal to:”+ myNumber);
document.write(“myString is equal to:”+ myString);
document.write(“myBoolean is equal to:”+ myBoolean);
</script>
</body>
</html>

Result:

mynumber is equal to:4

myString is equal to:variables are great

myBoolean is equal to:true 

The above result is displayed in browser and console

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s