![]() |
|
Tutorial Javascript tutorial 2 - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Design (https://sinister.li/Forum-Design) +--- Forum: Web Design (https://sinister.li/Forum-Web-Design) +--- Thread: Tutorial Javascript tutorial 2 (/Thread-Tutorial-Javascript-tutorial-2) |
Javascript tutorial 2 - Merkuri - 10-26-2012 I feel very bad today and i don't know how many errors i will have or how bad i will expain it but i hope you will undarstand it if you have problem ask in comments I hope that you have undarstand part 1 in this part we will cover Functions, If...Else, Switch Function A function is a block of code that will be executed when somebody call it, in JavaScript functions are very used Exaple Code: function MyFunction()
{
alert("My first function");
}between {} is the body of the function (the code we what to use later[when we call the function])) And now let's call it Code: <body>
<button onlick="MyFunction()">Function</button>
</body>This code is simple to undarstand i hope you deal with it Calling function with arguments When you call a function you can pass along some values to it these values are called arguments or parameters example Code: <body>
<button onclick="myFunction('Anarchy','Forums')">arguments</button>
<script>
function myFunction(name,name1)
{
alert("Welcome " + name + " to " + name1);
}
</script>
</body>Here we give values to name and name1 Code: <button onclick="myFunction('Anarchy','Forums')">arguments</button>and the function work with those 2 arguments you can have 5-10 even 1235135351325 arguments on function If...Else if statement - use this statement to execute some code only if a specified condition is true if...else statement - use this statement to execute some code if the condition is true and another code if the condition is false if...else if....else statement - use this statement to select one of many blocks of code to be executed switch statement - use this statement to select one of many blocks of code to be execute If statment example Code: if (time<18, time>13)
{
alert(Have a nice day);
}if the times is between 13 and 18 the program will alert (have a nice day) If...else Statement Code: if (time<20)
{
alert("Good day");
}
else
{
alert("Good evening");
}If its < than 20 it will alert good day if its > than 20 it will alert good evening If...else if...else Statement Code: if (time<10)
{
alert("Good morning");
}
else if (time<20)
{
alert("Good day");
}11
else
{
alert("Good evening");
}If the time is less than 10:00, you will get a Good morning alert if not but the time is less than 20:00 you will get a Good day alert otherwise you will get a Good evening alert Switch We use the switch statement to select one of many blocks of code to be executed Example Code: var day=new Date().getDay();
switch (day)
{
case 0:
x="Today it's Sunday";
break;
case 1:
x="Today it's Monday";
break;
case 2:
x="Today it's Tuesday";
break;
case 3:
x="Today it's Wednesday";
break;
case 4:
x="Today it's Thursday";
break;
case 5:
x="Today it's Friday";
break;
case 6:
x="Today it's Saturday";
break;
}Display today's weekday-name. Note that Sunday=0, Monday=1, Tuesday=2, etc Don't forget to write break; afther each case ! RE: Javascript tutorial 2 - Merkuri - 01-19-2013 (01-14-2013, 11:16 AM)john001 Wrote: Hey there. Thanks for this great info. I have problems in using switch but now after reading this it will easily solve my problem. Thanks. It's pleasure for me to hear that. RE: Javascript tutorial 2 - i0xIllusi0n - 03-10-2013 Loving these tutorials, basic syntaxes and everything. I know other languages so I know most of how everything will look, but I don't know Javascript as an overall language. You should write more of these
|