Module 2 Lesson 2
Module 2 Lesson 2
Learning Objectives:
Lesson Proper
As with algebra, JavaScript variables can be used to hold values (x=5) or expressions
(z=x+y).
Variable can have short names (like x and y) or more descriptive names (age, sum,
totalvolume)
Some common rules in using naming variable:
Variable names must begin with a letter
Variable names are case sensitive (y and Y are different variables)
Variable names must not be a reserve word, words used by the programming
language such as JavaScript keywords or Browser keywords in our case.
Variable names must not use special characters such as but not limited to *, (),
/, . , =, etc. except underscore _
JavaScript Keywords
1
Edukasyon Pantulay Pangkabuhayan (E2Ps) on ICT
MODULE 2: PROGRAMMING with JAVASCRIPT
Browser Keywords
Scope
The scope of an identifier is either
Global – An identifier that is accessible anywhere on the page
Local – Is accessible only within the function it is declared within
function someFunction() {
var counter = 0;
}
The identifier, counter, is local to the function and can only be used in that function.
However, the identifier, globalVariable, is not preceded by the var keyword and is thus a
global variable that can be used anywhere on the page, inside or outside of the function.
JavaScript is a dynamically typed language. The data type of the identifier is not assigned
when the identifier is declared. When a value is assigned to the identifier the identifier
2
Edukasyon Pantulay Pangkabuhayan (E2Ps) on ICT
MODULE 2: PROGRAMMING with JAVASCRIPT
takes on that type. The data type of the variable is not important until an operator is
applied to the variable. The behavior of the operator is dependent of the data type being
acted upon.
For example:
var name = “Sally”;
name = 34;
The string, Sally, is first assigned to the variable. Next, the integer 34 is assigned to the
variable. Both are legal but usage of the identifier is inconsistent. It is better if we are
consistent when assigning a data type to a variable. This leads to less confusing code.
The program below will compute for the sum of the values of 2 variables. It will also display
the values of the variables.
<script>
var x=5, y=2;
var sum=x+y;
document.write(“First number:” + x);
document.write(“<br>Second number: ” + y);
document.write(“<br>The sum is ” + sum);
</script>
Output
3
Edukasyon Pantulay Pangkabuhayan (E2Ps) on ICT
MODULE 2: PROGRAMMING with JAVASCRIPT
If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box
returns null.
Syntax
window.prompt("sometext","defaultText");
prompt("sometext","defaultText");
Sample:
<script>
var name=prompt("Enter your name","Name");
document.write("Welcome " + name);
</script>
Output:
4
Edukasyon Pantulay Pangkabuhayan (E2Ps) on ICT
MODULE 2: PROGRAMMING with JAVASCRIPT
The value inputted in a Prompt box is treated as string. If you want to treat the input
number as a number literal use Number() to convert the prompt value to number
Example:
var x=Number(prompt(“Enter a number”, “”));
5
Edukasyon Pantulay Pangkabuhayan (E2Ps) on ICT
MODULE 2: PROGRAMMING with JAVASCRIPT
Activity
I. Evaluate the following variable name whether valid or invalid.
1. Sum
2. 10percent
3. Street no
4. #five
5. num1
6. if
7. studentno.
8. 2num
9. average_of_a_b
10. product_of_x&y
6
Edukasyon Pantulay Pangkabuhayan (E2Ps) on ICT
MODULE 2: PROGRAMMING with JAVASCRIPT
Summary
JavaScript variables can be used to hold values (x=5) or expressions (z=x+y).
Variable scope is global and local.
JavaScript data types: Numbers, Booleans, Strings, Objects, Null, and
Undefined
Prompt box is one of the JavaScript Popup boxes that is used if you want the
user to input a value.
7
Edukasyon Pantulay Pangkabuhayan (E2Ps) on ICT