JavaScript(JS) is a programming language intended to add engaging interactive features to web pages.
Loading your JavaScript
Like CSS, JavaScript can be saved in an external .js file and linked in your HTML document with the <script> tag:
<scriptsrc="/script.js"></script>
Or it can be directly written inside the <script> tag:
<script>
// your js code here
</script>
It’s better to load your JavaScript code before the closing body tag.
<body>
<h1>Hello World!</h1>
<scriptsrc="/script.js"></script>
</body>
Using JavaScript Console
JavaScript is implemented in a web browser, so you don't need additional tools or setup to get going. Another quick and easy way to run JavaScript is in the web browser's developer tools using JavaScript Console. In this way, you can run JS on any website, but when you refresh the page, everything will be back to the original state.
The JavaScript console is in your developer tools panel:
Debugging
Programming code might contain syntax errors, or logical errors. JavaScript console is a very important tool to not only run but also debug your code. Sometimes there will be some error messages in your console, but sometimes there’s no error message to tell you where the errors are. You will use console.log() to test and identify the errors so that you can fix them:
console.log() will take whatever is inside the parentheses to log in to JavaScript console. So you can also use it to display messages in the console.
console.log("My JS file is loading successfully!");
For example, with the code above, the console prints this sentence in the parentheses out:
Comments in JavaScript
Single line comment:
// This is a single line comment
Multi-line comment:
/*
This is a
multi-line comment
*/
It’s always a good habit to use the comment to help you and others keep track of what this section of your code does.
Loading your JavaScript
<script src="/script.js"></script>
<script>
// your js code here
</script>
<body>
<h1>Hello World!</h1>
<script src="/script.js"></script>
</body>
Using JavaScript Console
Debugging
console.log("My JS file is loading successfully!");
Comments in JavaScript
// This is a single line comment
/*
This is a
multi-line comment
*/