Ways JavaScript work in HTML

Embedding JavaScript Programs in HTML Documents

Scripts can be added to HTML documents by embedding them in a script tag as shown below.

<h2>Embedded script tag</h2>
<script>
   alert('Hello World!');
</script>

Loading External JavaScript Files

Instead of embedding scripts right in the HTML document, a better alternative is to write the scripts in separate files and load them using the scripts tag.

Writing to Console

console.log('Hello World!');

Variables and Constants

Variables enable storing state information about applications.

console.log('Variables and Constants');
global1 = 10;
var functionScoped = 2;
let blockScoped = 5;
const constant1 = global1
                  + functionScoped
                  - blockScoped;

Variable Types

JavaScript declares several datatypes such as Number, String, Date, and so on.

console.log('Variable types');
let numberVariable = 123;
let floatingPointNumber = 234.345;
let stringVariable = 'Hello World!';
let booleanVariable = true;
let isNumber = typeof numberVariable;
let isString = typeof stringVariable;
let isBoolean = typeof booleanVariable;

Boolean Variables

console.log('Boolean Variables');
let true1 = true;
let false1 = false;
let false2 = true1 && false1;
let true2 = true1 || false1;
let true3 = !false2;
let true4 = numberVariable === 123;
let true5 = floatingPointNumber !== 321.432;
let false3 = numberVariable < 100;
let sortaTrue = '1' == 1
let notTrue   = '1' === 1