April 1, 2023

JavaScript Fundamental

JavaScript Where To


JavaScript Output

JavaScript can “display” data in different ways

JavaScript Print

JavaScript does not have any print object or print methods so you cannot access output devices from JavaScript.

But you can call the window.print() method to print the content of the current window.

1
2
3
4
5
6
7
8
9
10
11
12
<!DOCTYPE html>
<html>
<body>

<h2>The window.print() Method</h2>

<p>Click the button to print the current page.</p>

<button onclick="window.print()">Print this page</button>

</body>
</html>

JavaScript Statement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
let x, y, z;  // Statement 1
x = 5; // Statement 2
y = 6; // Statement 3
z = x + y; // Statement 4

// Let the browser to write "Hello Dolly." inside an HTML element with id="demo"
document.getElementById("demo").innerHTML = "Hello Dolly.";

// JavaScript ignores multiple spaces
let person = "Hege";
let person="Hege";

// JavaScript Code Blocks
function myFunction() {
document.getElementById("demo1").innerHTML = "Hello Dolly!";
document.getElementById("demo2").innerHTML = "How are you?";
}

JavaScript Syntax

1
2
3
4
5
6
7
8
// How to create variables:
var x;
let y;

// How to use variables:
x = 5;
y = 6;
let z = x + y;

The JavaScript syntax defines two types of values

Identifiers are JavaScript names, used to name variables and keywords, and functions.

A JavaScript name must begin with:

Numbers are not allowed as the first character in names.

Hyphens are not allowed in JavaScript. They are reserved for subtractions.

Eg.

Not Allowed

first-name, last-name, master-card, inter-city

Allowed

first_name, last_name, master_card, inter_city

FirstName, LastName, MasterCard, InterCity.

firstName, lastName, masterCard, interCity.


JavaScript Comments

1
2
3
4
5
6
7
// Single line comment

/*
Multiple
Line
Comment
*/

JavaScript Variables

4 ways to declare a variable:

Which way should I use to declare a variable?

Always declare JavaScript variables with var,let, orconst.

The var keyword is used in all JavaScript code from 1995 to 2015.

The let and const keywords were added to JavaScript in 2015.

If you want your code to run in older browsers, you must use var.

If you want a general rule: always declare variables with const.

If you think the value of the variable can change, use let.

1
2
3
4
5
6
7
8
9
10
11
const price1 = 5;
const price2 = 6;
let total = price1 + price2;

// Multi line declaration
let person = "John Doe",
carName = "Volvo",
price = 200;

// A variable declared without a value will have the value undefined
let carName; // Here carName have the value undefined

Re-Declaring Variables

You cannot re-declare a variable declared with let or const

Eg.

1
2
let carName = "Volvo";
let carName;

JavaScript Let

1
2
3
4
5
6
7
// Not working
let x = "John Doe";
let x = 0;

// Works
var x = "John Doe";
var x = 0;

Block Scope

Before ES6 (2015). JavaScript had Global Scope and Function Scope.

ES6 introduced two important new JavaScript keywords: let and const.

These two keywords provide Block Scope in JavaScript.

Variables declared inside a { } block cannot be accessed from outside the block

Variables declared with the var keyword can NOT have block scope.

Variables declared inside a { } block can be accessed from outside the block.

1
2
3
4
5
6
{
let x = 2;
var y = 2;
}
// x can NOT be used here
// y CAN be used here

About this Post

This post is written by Andy, licensed under CC BY-NC 4.0.

#JavaScript