What :
Javascript is a programming language which can exist on its own and it can interact with the DOM elements. It goes well with HTML and CSS to give so many good things in the web page. These are all facts about the language. Now we can see about some working in that language.
To declare any variable in this language we can use the "var" keyword.
- var name="prem"; // String type has double quotes( "" )
- var age=15;// Numeric type don't have double quotes.
- var nothing; //undefined type
- var something=null; // null type
To create and call a function.
var add = function(a,b) // function definition
{
return a+b;
};
add(10,15); // invoking the function
The most used output function is,
document.write("something");
To print the output onto the console we use
console.log( add(10,15) );
To print the output onto the screen as a pop-upconfirm( add(10,15) ); // here the return will be a boolean
To output without the use of a return value we can use alert box
alert( add(10,15) );
To get inputs directly from the user we use the prompt(); function.
It takes two parameter first is the text you need to appear on the screen and the second is the default value in case user doesn't enter anything.
var name=("type your name","prem");
The conditional statements and the looping statements will work in the same way as in C language.
Note : There is a difference between operators "==" and "===".
E.g;
5=="5" and 5==5 will return true
but
5==="5" returns false while 5===5 returns true. i.e, here both the value and type are checked.
Objects:
Javascript supports objects. An object have properties and methods.
For example for a string,
"prem".length // is a property
and
"prem" .toUpperCase() // is a method.
Method is doing something over the given object while properties are the attributes associated with an object.
Date is an object in the JS(shorthand for JavaScript) and the date object can be obtained by,
var today = new Date ();
Now, to have the seperate date, hours, minutes and second we can use the code below,var date=today.getDate();
var month=today.getMonth();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
No comments:
Post a Comment