Introduction to JavaScript
Let's understand what is JavaScript?
JavaScript is a scripting or programming language that allows you to implement complex features on web pages.
JavaScript is HIGH-LEVEL Object- oriented programming, Multi-Paradigm programming language.
Getting Started with Hello World
Hello world is very basic program to start with:
console.log("Hello World!");
How many ways JavaScript can be added in browser??
Using Script tag in HTML
- In html file you can use
<script></script>
tag and under that write Javascript code.
=> Let's see an example:
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
</head>
<body>
<h2>Demo JavaScript in Head</h2>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>
</body>
</html>
Individual .js file
- JavaScript can be added with
<script> </script>
tag. - Javascript should be linked at last of line as code works line by line and first html css will be loaded and then script will be loaded.
- src attribute is used for giving source of file i.e what is file name and which location.
=> Suppose file is named as index.html:
<!-- Html File -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Javascript file added</title>
</head>
<body>
<p> JavaScript Loaded </p>
<script src="./app.js"></script>
</body>
</html>
JavaScript File is:
// app.js file
document.write("Hello I am Nishtha!!");
Output
It will print:
JavaScript Loaded
Hello I am Nishtha!!