Code Skiller logoCB Logo
Logo LearnLearnLogo PracticePracticeLogo HireHireLogo IDEIDE

How to Run JavaScript Code

User image

Published by

sanya sanya

Published at: 6th May, 2023
2.825 mins read

The primary way of writing and executing the JavaScript code is to write it in the "Script" of the HTML Document.

The example of the JavaScript written in the HTML Document using the Script tag is mentioned below -

<body>
<p>This is the line before adding JavaScript</p>
<script>
console.log( 'Hello world!' );
</script>
<p>This is the line after adding javascript...</p>

</body>
</html>

External Scripts

There is an alternate method of writing and executing JavaScript in the HTML Document. The method is for inserting external scripts with the script tag using the src attribute.

// < script src="/js/script1.js">

Scripts: Async and Defer

Most importantly in modern JavaScript applications or websites, the scripts sometimes got heavier than the HTML Document which makes the performance down and downloading time higher.

When the HTML Document gets loaded by the Browser and the script src tag, it can go on with the DOM Building and the script must be loaded as soon it gets countered in the HTML Document execution. The same goes with the Javascript in the script tag without the "src" attribute. The browser runs this and then the further execution of the HTML Document proceeds.

In such case Script tag is like -



However, this leads to two problems to rise and both are mentioned below -
1- Script add handlers because they can't see the DOM Elements below them.
2- The user cannot see the content of the page until and unless the script gets loaded.

The solution for that is the Defer.

Defer

The Defer is an attribute that tells the browser not to wait for the script tag to be loaded. However, the browser will continue to execute the HTML Document. The code for the same is mentioned below -

<p>...content before script...</p>
<script defer src="main.js"></script>

<!-- visible immediately -->
<p>...content after script...</p>

Defer is like not waiting for the script to be loaded -

Async

Async is an attribute that let the javascript code inside the script tag load "asynchronously". If the script is async it means that it will load independently of the other scripts.

Both the attributes "defer" and "async" are somewhat the same. Defer also make the script "non-blocking".

Comments in Javascript

Comments in javascript are the lines of code or simple language written to explain the code which doesn't affect the execution of the code. There are two types of comments supported by javascript.

Single Line Comment

These comments are single-liner which indicates the code or message which will not affect the execution of the code. It starts with the sign "//" and the example for the same is as follows -

//program to add two numbers let a = 5; let b = 2; console.log(a + b);
Output: 7

Multiline Comment

Multiline Comments have the same functionality and the only difference is that it is used to comment more than one line at once. It is started with "/" and ends at "/". An example of the same is mentioned below -

/* let a = 5; let b = 2; console.log(a + b); */

The above code will not provide any output because the whole program is written under comment. Hence, the program will not execute.

Library

WEB DEVELOPMENT

Basic

HTML - Hyper Text Markup Language

CSS - Cascading Style Sheets

JavaScript

An Introduction to Javascript!

How to Run JavaScript Code

Variables in Javascript

Numbers in JavaScript

JavaScript Operators

Data Types in JavaScript

Conditional Statements

Switch Statements

Loops in Javascript

Arrays in JavaScript

Strings in JavaScript

Objects in JavaScript

Object Methods in JavaScript

Functions in JavaScript

Object Referencing and Copying in JavaScript

' this' keyword

Asynchronous Programming in JavaScript

Callbacks in JavaScript

Promises in JavaScript

Constructor Functions in JavaScript

Async and Await in JavaScript

Type Conversion in Javascript

DOM

Currying in JavaScript

Network Request

Frontend

Backend

Interview Questions

FAANG QUESTIONS

On this page

Comments in Javascript

Single Line Comment

Multiline Comment