Professional Documents
Culture Documents
JS 1
JS 1
JS 1
Dr. A. Beulah
AP/CSE
• Web pages are not the only place where JavaScript is used. Many
desktop and server programs use JavaScript.
<h2>JavaScript</h2>
<button type="button"
onclick='document.getElementById(“ex").innerHTML =
"Hello You are learning JavaScript!"'>Click Me!</button>
</body>
</html>
<button type="button"
onclick="document.getElementById('demo').
innerHTML = 'Hello JavaScript!'">Click
Me!</button>
</body>
</html>
<h2>JS -- styles</h2>
</body>
</html>
<button type="button"
onclick="document.getElementById('demo')
.style.display='none'">Hide Me!</button>
</body>
</html>
Feb 15, 2022 A. Beulah Unit II 8
CAN SHOW HTML ELEMENTS
• Showing hidden HTML elements can also be done by changing
the display style
<html>
<body>
<p>Show hidden element!</p>
<p id="demo" style="display:none">I am
hidden element</p>
<button type="button"
onclick="document.getElementById('demo')
.style.display='block'">Show hidden
element</button>
</body>
</html>
Feb 15, 2022 A. Beulah Unit II 9
WHERE TO WRITE JS
• The <script> Tag
– In HTML, JavaScript code is inserted between <script> and </script>
tags.
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "My First JavaScript";}
</script>
• JavaScript Functions and Events
– A JavaScript function is a block of JavaScript code, that can be
executed when "called" for.
– For example, a function can be called when an event occurs, like when
the user clicks a button.
myScript.js
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
<script src="https://www.w3schools.com/js/myScript1.js"></script>
script located in a specified folder on the current web site
<script src="/js/myScript1.js"></script>
script located in the same folder as the current page
<script src="myScript1.js"></script>
• total_number_of_students = 984 ;
• swapFlag = false ;
• x = y + 33 ;
Feb 15, 2022 A. Beulah Unit II 27
ARITHMETIC OPERATORS
Multiply 2*4→8
Divide 2 / 4 → 0.5
Modulus 5%2→1
Add 2+4→6
Subtract 2 - 4 → -2
Negate -(5) → -5
Feb 15, 2022 A. Beulah Unit II 28
COMPARISON OPERATORS
Not the same as the
assignment “=” operator
if ( today == “Sunday” )
document.write(“The shop is closed”);
a = 23 ;
quotient = floor( a / 2) ;
remainder = a % 2 ;
<p>When adding a string and a number, JavaScript will treat the number as a
string.</p>
<p id="demo"></p>
<script>
var x = "Volvo" + 16;
document.getElementById("demo").innerHTML = x;
</script>
<p id="demo"></p>
<script>
var cars = ["Saab","Volvo","BMW"];
document.getElementById("demo").innerHTML = cars[0];
</script>
<p id="demo"></p>
<script>
var person = {
firstName : "John",
lastName : "Doe",
age : 50,
eyeColor : "blue"
};
document.getElementById("demo").innerHTML =
person.firstName + " is " + person.age + " years old.";
</script>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
typeof undefined + "<br>" +
typeof null + "<br><br>" +
(null === undefined) + "<br>" +
(null == undefined);
</script>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
typeof {name:'john', age:34} + "<br>" +
typeof [1,2,3,4] + "<br>" +
typeof null + "<br>" +
typeof function myFunc(){};
</script>