JS Basics Test
- How to use JS in your WebPage
- What is Javascript used for?
- Veriables in JavaScript
- Data types
- DOM Manipulation
How to use JS in your WebPage
Whenever you want to use Javascript on your Webpage you need to inset the JavaScript code inbetween two script codes
Example below
<script> </script>
What is Javascript used for?
JavaScript is mainly used to enhance the interactivity and functionality of webpages. JavaScript and HTML have differene uses, and when utilized properly it can properly complete your webpage. HTML is used to create the content of your Webpage which includes, words, images, lists, and basic structure of a webpage
Veriables in JavaScript
Variable are basic code and commands in javascript that have different functionalities.
var myVariable = 10;
var myString = "Hello, World!";
Data types
Data types are used for mathematical and logical operation so there are no errors.
var num = 42;
var greeting = "Hello!";
var isTrue = true;
var myArray = [1, 2, 3, 4];
var person = { name: "John", age: 30 };
Functions
Funtions are blocks of code that can be defined and then called by name. They can take parameters and return values.
function add(a, b) {
return a + b;
}
var result = add(3, 5); // result will be 8
Control Structures
JavasScript includes typical control structures like loops and conditional statements
for (var i = 0; i < 5; i++) {
console.log(i); // prints 0 to 4
}
if (num > 0) {
console.log("Number is positive");
} else {
console.log("Number is non-positive");
}
DOM Manipulation: JavaScript can be used to manipulate the Document Object Model (DOM) of a web page, allowing you to change the content and appearance of elements on the page.
javascript
Copy code
// HTML: <div id="myDiv">Hello, World!</div>
var element = document.getElementById("myDiv");
element.innerHTML = "Hello, JavaScript!";
To use JavaScript in a web page, you can include your JavaScript code inside <script> tags in the HTML file, either in the <head> or <body> section.
Example:
html
Copy code
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Example</title>
<script>
function myFunction() {
alert("Hello, JavaScript!");
}
</script>
</head>
<body>
<button onclick="myFunction()">Click Me!</button>
</body>
</html>
In this example, when the button is clicked, the myFunction() JavaScript function is called, displaying an alert with the message "Hello, JavaScript!"
DOM Manipulation
JavaScript can be used to manipulate the Document Object Model (DOM) of a web page, allowing you to change the content and appearance of elements on the page.
// HTML: <div id="myDiv">Hello, World!</div>
var element = document.getElementById("myDiv"
To use this on a web page include Javascripit code inside the script and below when myfunction is clicked JavaScript will display Hello JavaScript.
<html>
<head>
<title>JavaScipt Examples</title>
<script>
function myFunction() {
alert("Hello, JavaScript!")
}
</script>
</head>
<body>
<button onclick="myFunction()">Click Me!</button>
</body>
</html>