Start Code in JavaScript. #JS-Shorts [7]

Start writing Code in JavaScript. JS-Shorts Start Now Free Course. For any one wish to learn JavaScript. #Code #Debug #Test and #Release.

Start Code in JavaScript. #JS-Shorts [7]

In order to write JavaScript code, we need a script element.

  • The <script> element can be added in <head> section or in the <body> section of HTML page.
  • But the best practice is to put the <script> element at the end of the <body> section. Because browser parse the code from top-to-bottom and other HTML element need to be executed before the execution of JavaScript code.
  • Type script and press tab - this will add <script> element. (Check video below↓)
  • Add console.log("Hello World!"); and save.
  • Open up chrome - right click - then click inspect - then open console tab - and see the log "Hello World!". See the video below
  • console.log("Hello World!");
  • Here "Hello World!" is a string in JavaScript.

It's best practice to write comments for the code - to define why this code is written and How it works. It help other developeres to understand the code.


External JS file

In a real world applications we have thousands line of code - and we can't mix JavaScript code with HTML code. That is the reason we separate JavaScript code in a file and import it in HTML file. See the video below↓ (we can have as many JS files as we want.)


In this article we have covered:

  1. To write code inside HTML file within <script> tag.
  2. To write code directly inside JavaScript file.

Next

Run JavaScript in using Node. #JS-Shorts [8]
Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on the Chrome V8 engine and executes JavaScript code outside a web browser.