• Home
  • ES6
  • ADVANCED
    • Javascript Array Methods
    • Javascript String Methods
    • Javascript Regex
    • ES Next
  • JS BOM
  • JS DOM
  • WEB API
  • SNIPPETS
  • TYPESCRIPT
  • Golang
  • Dark Mode Light Mode
  • » Javascript Fundamental. » JavaScript Hello World
    <h1 id="javascript-hello-world-example">JavaScript Hello World Example</h1> <p><strong>Summary</strong>: This tutorial helps you get started with JavaScript by showing you how to embed JavaScript code into an HTML page.</p> <p>To insert JavaScript into an HTML page, you use the <code>&lt;script&gt;</code> element. There are two ways to use the <code>&lt;script&gt;</code> element in an HTML page:</p> <ul> <li>Embed JavaScript code directly into the HTML page.</li> <li>Reference an external JavaScript code file.</li> </ul> <h2 id="embed-javascript-code-in-an-html-page">Embed JavaScript code in an HTML page</h2> <p>Placing JavaScript code inside the <code>&lt;script&gt;</code> element directly is not recommended and should be used only for proof of concept or testing purposes.</p> <p>The JavaScript code in the <code>&lt;script&gt;</code> element is interpreted from top to bottom. For example:</p> <pre><code class="hljs language-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-keyword">html</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>JavaScript Hello World Example<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="language-javascript"> <span class="hljs-title function_">alert</span>(<span class="hljs-string">&#x27;Hello, World!&#x27;</span>); </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>Code language: HTML, XML (xml) </code></pre> <p>In the <code>&lt;script&gt;</code> element, we use the <code>alert()</code> function to display the <code>Hello, World!</code> message.</p> <h2 id="include-an-external-javascript-file">Include an external JavaScript file</h2> <p>To include a JavaScript from an external file:</p> <ul> <li>First, create a file whose extension is <code>.js</code> e.g., <code>app.js</code> and place it in the <code>js</code> subfolder. Note that placing the JavaScript file in the <code>js </code>folder is not required however it is a good practice.</li> <li>Then, use the URL to the JavasScript source code file in the <code>src</code> attribute of the <code>&lt;script&gt;</code> element.</li> </ul> <p>The following shows the contents of the app.js file:</p> <pre><code>alert(&#x27;Hello, World!&#x27;);Code language: JavaScript (javascript) </code></pre> <p>And the following shows the <code>helloworld.html</code> file:</p> <pre><code class="hljs language-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-keyword">html</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>JavaScript Hello World Example<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;js/app.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>Code language: HTML, XML (xml) </code></pre> <p>If you launch the <code>helloworld.html</code> file in the web browser, you will see an alert that displays the <code>Hello, World!</code> message.</p> <p>Note that you can load a JavaScript file from a remote server. This allows you to serve up JavaScript from various domains e.g., content delivery network (CDN) to speed up the page.</p> <p>When you have multiple JavaScript files on a page, the JavaScript engine interprets the files in the order that they appear. For example:</p> <pre><code class="hljs language-html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;js/service.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;js/app.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>Code language: HTML, XML (xml) </code></pre> <p>In this example, JavaScript engine will interpret the <code>service.js</code> and the <code>app.js</code> files in sequence. It completes interpreting the <code>service.js</code> file first before interpreting the <code>app.js</code> file.</p> <p>For the page that includes many external JavaScript files, the blank page is shown during the page rendering phase.</p> <p>To avoid this, you include the JavaScript file just before the <code>&lt;/body&gt;</code> tag as shown in this example:</p> <pre><code class="hljs language-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-keyword">html</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>JavaScript Hello World Example<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span> <span class="hljs-comment">&lt;!-- end of page content here--&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;js/service.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;js/app.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>Code language: HTML, XML (xml) </code></pre> <h2 id="the-async-and-defer-attributes">The async and defer attributes</h2> <p>To change how the browser load and execute JavaScript files, you use one of two attributes of the <code>&lt;script&gt;</code> element <code>async</code> and <code>defer</code>.</p> <p>These attributes take effect only on the external script files. The <code>async</code> attribute instructs the web browser to execute the JavaScript file asynchronously. The <code>async</code> attribute does not guarantee the script files to execute in the order that they appear. For example:</p> <pre><code class="hljs language-html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">async</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;service.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">async</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;app.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>Code language: HTML, XML (xml) </code></pre> <p>The <code>app.js</code> file might execute before the <code>service.js</code> file. Therefore, you must ensure that there is no dependency between them.</p> <p>The <code>defer</code> attribute requests the web browser to execute the script file after the HTML document has been parsed.</p> <pre><code class="hljs language-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-keyword">html</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">&quot;en&quot;</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;UTF-8&quot;</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>JavaScript defer demonstration<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">defer</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;defer-script.js&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span> <span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>Code language: HTML, XML (xml) </code></pre> <p>Even though we place the <code>&lt;script&gt;</code> element in the <code>&lt;head&gt;</code> section, the script will wait for the browser to receive the closing tag <code>&lt;html&gt;</code> to start executing.</p> <h2 id="summary">Summary</h2> <ul> <li>Use <code>&lt;script&gt;</code> element to include a JavaScript file in a HTML page.</li> <li>The <code>async</code> attribute of the <code>&lt;script&gt;</code> element instructs the web browser to fetch the JavaScript file in parallel and then parse and execute as soon as the JavaScript file is available.</li> <li>The <code>defer</code> attribute of the <code>&lt;script&gt;</code> element allows the web browser to execute the JavaScript file after the document has been parsed.</li> </ul>

    GETTING STARTED

  • What is JavaScript
  • Install a JavaScript Code Editor
  • Meet the Console Tab of Devtools
  • JavaScript Hello World
  • JAVASCRIPT FUNDAMENTALS

  • Syntax
  • Variables
  • Data Types
  • Number
  • Numeric Separator
  • Octal and Binary Literals
  • Boolean
  • String
  • Object
  • Primitive vs. Reference Values
  • Array
  • JAVASCRIPT OPERATORS

  • Arithmetic Operators
  • Remainder Operator
  • Assignment Operators
  • Unary Operators
  • Comparison Operators
  • Logical Operators
  • Logical Assignment Operators
  • Nullish Coalescing Operator
  • Exponentiation Operator
  • CONTROL FLOW

  • if
  • if else
  • if else if
  • Ternary Operator (:?)
  • switch case
  • while
  • do while
  • for
  • break
  • continue
  • Comma Operator
  • JAVASCRIPT FUNCTIONS

  • Functions
  • Functions are First-Class Citizens
  • Anonymous Functions
  • Understanding Pass-By-Value in JavaScript
  • Recursive Functions
  • Default Parameters
  • JAVASCRIPT OBJECTS

  • Object Methods
  • Constructor Functions
  • Prototype
  • Constructor/Prototype Pattern
  • Prototypal Inheritance
  • this
  • globalThis
  • Object Properties
  • for…in Loop
  • Enumerable Properties
  • Own Properties
  • Object.values()
  • Object.entries()
  • Object.is()
  • Factory Functions
  • Object Destructuring
  • Optional Chaining Operator
  • Object Literal Syntax Extensions
  • CLASSES

  • Class
  • Getters & Setters
  • Class Expressions
  • Computed Properties
  • Inheritance
  • new.target
  • Static Methods
  • Static Properties
  • Private Fields
  • Private Methods
  • instanceof Operator
  • ADVANCED FUNCTIONS

  • Function Type
  • The call() Method
  • The apply() method
  • The bind() Method
  • Closures
  • Immediately Invoked Function Expression (IIFE)
  • Returning Multiple Values
  • Arrow Functions
  • Arrow Functions: Do & Don’t
  • Rest Parameters
  • Callback
  • PROMISES & ASYNC/AWAIT

  • Promises
  • Promise Chaining
  • Promise.all()
  • Promise.race()
  • Promise.any()
  • Promise.allSettled()
  • Promise.prototype.finally()
  • Promise Error Handling
  • async/await
  • JAVASCRIPT RUNTIME

  • Execution Context
  • Call Stack
  • Event Loop
  • Hoisting
  • Variable Scopes
  • About Me

    • Made by Rasel Mahmud
    • Portfolio

    Top References

    • javascripttutorial.net
    • W3school.com

    Top Tutorials

    • Primitive vs. Reference Values
    • What is JavaScript

    NOTE:

    This website only for Developing purpose not for business. and all content copied from other place like javascripttutorial.net and W3school.com