Java Script - 2
Java Script - 2
JavaScript and Java are completely different languages, both in concept and
design.
<html>
<body>
<h2>JavaScript Statements</h2>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
function myFunction() {
</script>
</body>
</html>
JavaScript Keywords
JavaScript statements often start with a keyword to identify the JavaScript
action to be performed.
Keyword Description
debugger Stops the execution of JavaScript, and calls (if available) the
debugging function
do ... while Executes a block of statements, and repeats the block, while a
condition is true
<html>
<body>
<h1 id="myH"></h1>
<p id="myP"></p>
<script>
// Change heading:
// Change paragraph:
</script>
</body>
</html>
<html>
<body>
<h1 id="myH"></h1>
<p id="myP"></p>
<script>
/*
in my web page:
*/
</script>
</body>
</html>
JavaScript Variables
JavaScript variables are containers for storing data values.
<html>
<body>
<h2>JavaScript Variables</h2>
<p id="demo"></p>
<script>
var x = 5;
var y = 6;
var z = x + y;
document.getElementById("demo").innerHTML = z;
</script>
</body>
</html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p id="demo"></p>
<script>
var price1 = 5;
var price2 = 6;
document.getElementById("demo").innerHTML =
</script>
</body>
</html>
JavaScript Identifiers
All JavaScript variables must be identified with unique names.
Identifiers can be short names (like x and y) or more descriptive names (age,
sum, totalVolume).
The general rules for constructing names for variables (unique identifiers) are:
<html>
<body>
<h2>JavaScript Variables</h2>
<p id="demo"></p>
<script>
var pi = 3.14;
document.getElementById("demo").innerHTML = pi;
//document.getElementById("demo").innerHTML = person;
//document.getElementById("demo").innerHTML = answer;
</script>
</body>
</html>
<html>
<body>
<h2>JavaScript Variables</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = carName;
</script>
</body>
</html>
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
-- Decrement
<html>
<body>
<p>A typical arithmetic operation takes two numbers (or expressions) and produces a new number.</p>
<p id="demo"></p>
<script>
var a = 3;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
<html>
<body>
<h2>The -= Operator</h2>
<p id="demo"></p>
<script>
var x = 10;
x -= 5;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
<html>
<body>
<h2>The %= Operator</h2>
<p id="demo"></p>
<script>
var x = 11;
x %= 5;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>