Login Register






Tutorial Javascript tutorial 2 filter_list
Author
Message
Javascript tutorial 2 #1
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"); }
On the first line we give a name to the function (MyFunction in the example)
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 !

Reply

RE: Javascript tutorial 2 #2
(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.

Reply

RE: Javascript tutorial 2 #3
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 Smile
[Image: BAvhP6h.png]
Code:
[5:42:25 PM] i0xillusi0n: Breshie don't bust a nut over chloe now [5:42:31 PM] Entity: fapfapfapfapfapfapfapfapfapfap [5:42:33 PM] Jigglypuff | SL: EWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW

Reply