Web Development
HTML Course
CSS Course
JavaScript Course
PHP Course
Python Course
SQL Course
SEO Course

Introduction to JavaScript

What is JavaScript?

JavaScript is the language that makes web pages interactive. If HTML is the structure and CSS is the style, JavaScript controls the behavior — what happens when the user interacts with the page.

Writing your first JavaScript script

JavaScript is written between <script> tags. The code can be included directly on the page or in a separate file.

🔧 Live Example: First JavaScript Script

The browser console – What it is and how to use it?

The console is a developer tool that shows us what happens “behind the scenes”. Here we can see messages, errors, and test JavaScript code.

How to open it:

Example of code that writes to the console:

🔧 Live Example: Console.log

Visual alternative: Display directly on the page

For beginners, it is more intuitive to see the result directly on the page, not just in the console.

🔧 Live Example: Display on Page

Comments in JavaScript

Comments are explanations in the code that are not executed. We use them for clarity or to temporarily disable a part of the code.

🔧 Live Example: Comments

Data types: String and Number

JavaScript works with different types of information, called data types. The most common are:

🔧 Live Example: Data Types

Variables and naming conventions

A variable is like a box where we store a value. It is declared with let, const, or var.

🔧 Live Example: Variables

What is camelCase?

camelCase is a writing convention where:

Example:

let fullName = "John Smith";

It is called “camelCase” because it looks like a camel's hump: it starts low and rises with each new word.

🔧 Live Example: camelCase

Special Data Types in JavaScript

Boolean – True or False

A boolean data type has only two possible values: true or false.

It is used to check conditions, control program logic, and answer "yes" or "no" questions.

let isLoggedIn = true;
let hasCorrectPassword = false;

We can use these values in conditions:

if (isLoggedIn) {
  console.log("Welcome!");
}
🔧 Live Example: Boolean

Null – Intentional absence of a value

null is a special value that means "nothing" or "empty", but intentionally set by the programmer.

It is used when we want to indicate that a variable does not yet have a value, but will receive one later.

let result = null; // currently no result

Later we can update it:

result = "Success!";
🔧 Live Example: Null

Undefined – Declared but unassigned variable

undefined appears automatically when:

let name;
console.log(name); // will display "undefined"

It is different from null, which is intentionally set. undefined appears by default.

🔧 Live Example: Undefined

Comparing Null and Undefined

Although both mean "no value", they are different:

let a = null;
let b;
console.log(a === b); // false

In JavaScript, null and undefined are treated differently in strict comparisons (===).

Logical Operators in JavaScript

Logical operators are used to combine or compare boolean expressions. The result is always true or false.

The AND Operator – &&

Returns true only if both conditions are true.

let isLoggedIn = true;
let hasAccess = true;

if (isLoggedIn && hasAccess) {
  console.log("✅ Access granted");
}
🔧 Live Example: AND Operator

The OR Operator – ||

Returns true if at least one of the conditions is true.

let hasInvitation = false;
let isVIP = true;

if (hasInvitation || isVIP) {
  console.log("✅ You may enter");
}
🔧 Live Example: OR Operator

The NOT Operator – !

Inverts the value: !true becomes false, and !false becomes true.

let isTired = false;

if (!isTired) {
  console.log("✅ You can continue working");
}
🔧 Live Example: NOT Operator

Combining Logical Operators

We can combine multiple operators for complex conditions:

let isLoggedIn = true;
let hasAdminRole = false;
let correctPassword = true;

if ((isLoggedIn && correctPassword) || hasAdminRole) {
  console.log("✅ Access granted");
}

Parentheses help group conditions logically and avoid ambiguity.

Comparison Operators in JavaScript

Comparison operators are used to compare two values. The result is always true or false.

Loose equality – ==

Compares two values after type conversion. That is, if the types are different, JavaScript tries to convert them.

console.log(5 == "5"); // true – automatic conversion

Strict equality – ===

Compares both value and type. Recommended for precision.

console.log(5 === "5"); // false – different types
🔧 Live Example: Equality

Difference – != and !==

!= checks if values are different (with conversion), while !== checks difference without conversion.

console.log(5 != "5");   // false
console.log(5 !== "5");  // true

Less / Greater

The operators <, >, <=, >= compare numbers:

let x = 10;
let y = 7;

console.log(x > y);   // true
console.log(x <= y);  // false
🔧 Live Example: Interactive Numeric Comparison

Recommendation

Use === and !== instead of == and != to avoid errors caused by automatic conversions.

Conditional Statements in JavaScript

Conditional statements allow the code to make decisions based on certain conditions. The most common are:

The if Statement

Checks a condition and executes code only if it is true.

let temperature = 30;

if (temperature > 25) {
  console.log("🌞 It's warm outside!");
}
🔧 Live Example: if

The if...else Statement

Executes one block of code if the condition is true, another if it is false.

let hour = 21;

if (hour < 20) {
  console.log("🕒 It\'s still early");
} else {
  console.log("🌙 It's already evening");
}
🔧 Live Example: if...else

The if...else if...else Statement

Allows checking multiple successive conditions.

let grade = 8;

if (grade >= 9) {
  console.log("🏆 Excellent");
} else if (grade >= 7) {
  console.log("👍 Good");
} else {
  console.log("📘 You still have more to learn");
}

The switch Statement

It is useful when we have multiple distinct options to check.

let day = "monday";

switch (day) {
  case "monday":
    console.log("📅 The week begins");
    break;
  case "friday":
    console.log("🎉 The weekend is near");
    break;
  default:
    console.log("⏳ Regular day");
}
🔧 Live Example: switch

String Methods and Template Literals

In JavaScript, strings are used to represent text. We can apply useful methods to analyze, modify, or combine them.

Common string methods

Here are some of the most commonly used methods:

Method Description Example
length Returns the length of the string "Hello".length5
toUpperCase() Converts all letters to uppercase "hello".toUpperCase()HELLO
toLowerCase() Converts all letters to lowercase "HELLO".toLowerCase()hello
includes() Checks if a text is contained in the string "Hello world".includes("world")true
replace() Replaces part of the text "Anna has apples".replace("apples", "pears")Anna has pears
trim() Removes spaces from the start and end " text ".trim()"text"
🔧 Live Example: String Methods

Template Literals – Flexible strings with backticks

Template literals are strings enclosed with `backticks` (grave accent) that allow:

let name = "John";
let message = `Hello, ${name}!
Welcome to the JavaScript tutorial.`;
console.log(message);
🔧 Live Example: Template Literals

Recommendation

Use `template literals` instead of classic concatenation (+) for clearer and easier-to-maintain code.


🧠 Quiz – JavaScript Introduction

Top