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.
- When you click a button and a message appears.
- When the image changes when you hover over it.
- When the form automatically checks if you filled it correctly.
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.
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:
- Press F12 on the keyboard.
- Or right-click on the page → Inspect → Console tab.
Example of code that writes to the console:
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.
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.
Data types: String and Number
JavaScript works with different types of information, called data types. The most common are:
- String – text between quotes:
"Hello" - Number – integers or decimals:
42,3.14
Variables and naming conventions
A variable is like a box where we store a value. It is declared with let, const, or
var.
What is camelCase?
camelCase is a writing convention where:
- The first word starts with a lowercase letter.
- Each new word starts with an uppercase letter.
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.
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!");
}
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!";
Undefined – Declared but unassigned variable
undefined appears automatically when:
- We declare a variable but do not assign a value.
- We try to access a property that does not exist.
let name; console.log(name); // will display "undefined"
It is different from null, which is intentionally set. undefined appears by default.
Comparing Null and Undefined
Although both mean "no value", they are different:
null– value set by the programmer.undefined– value implicit when nothing has been set.
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");
}
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");
}
The NOT Operator – !
Inverts the value: !true becomes false, and !false becomes
true.
let isTired = false;
if (!isTired) {
console.log("✅ You can continue working");
}
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
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
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:
if– executes code if a condition is trueelse– executes code if the condition is falseelse if– checks another condition if the first was falseswitch– checks multiple possible cases
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!");
}
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");
}
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");
}
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".length → 5 |
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" |
Template Literals – Flexible strings with backticks
Template literals are strings enclosed with `backticks` (grave accent) that allow:
- Variable interpolation using
${} - Writing across multiple lines
let name = "John";
let message = `Hello, ${name}!
Welcome to the JavaScript tutorial.`;
console.log(message);
Recommendation
☞ Use `template literals` instead of classic
concatenation (+) for clearer and easier-to-maintain code.
