Programmers use variables to store values. A variable can hold several types of data. In JavaScript you don'y have to declare a variable's data type before using it. Any variable can hold any JavaScript data type, including :
- String data
- Numbers
- Boolean values (T/F)
Variable Names
There are rules and conventions is naming variables in any programming language. It's good practice to use descriptive names for variables. The following are the JavaScript rules :
- The variable name must start with a letter or an underscore. firstName or _myName
- You can use numbers in a variable name, but not as the first character. name01 or tuition$
- You can't use space to separate characters. userName not user Name
- Capitalize the first letter of every word except the first. salesTax or userFirstName
Variables
- To declare variables, use the keyword var and variable name :
- To assign values to variables, add an equal sign and the value :
var userName="Smith" var price=100
Functions
With functions, you can give a name to a whole block of code, allowing you to reference it from anywhere in your program. JavaScript has built-in functions for several predefined operations. Here are three some functions.
With functions, you can give a name to a whole block of code, allowing you to reference it from anywhere in your program. JavaScript has built-in functions for several predefined operations. Here are three some functions.
- alert("message")
- confirm("message")
- prompt("message")
Functions Example
- Open "functions.html" and View the Source
- Put the cursor after "// add code here" and enter :
var userName var willDoSurvey userName=prompt("Enter you name","") alert("Thank you," + userName)
- Save the changes and Refresh the page
User-Defined functions
With user-defined functions, you can name a block of code and call it when you need it. You define a function in the HEAD selection of a web page. It's defined with the function keyword, followed by the function name and any arguments.
function functionName(argument)
{ statements }
SEE VIDEO
User-Defined Example
- Open "userdefined.html" and View yhe Source
- Put the cursor after "<!- enter function -->" and enter :
<SCRIPT language="JavaScript"> function showAlert() { alert("this is a user-defined function.") } </SCRIPT>
- In the BODY, put the cursor after "<! enter the button def here ->" and enter :
<INPUT type-"button" value="Run the Function" onClick="showAlert()">
- Save the changes and Refresh the browser.
RUN THE FUNCTION
SEE VIDEO
3 komentar:
Hello, Im newbie in this tutorial
hmmmm, maybe we can leaning together
nice to meet you Dhio
OK, Well I will try to to the best in this learning.
Post a Comment