What is JavaScript? 💡
JavaScript (JS) is a dynamic programming language that is most often used to create interactive web pages. However, JS is also great for automating tasks, working with data, and creating server applications. 🔥
Unlike languages that require compilation (for example, C++ or Java), JavaScript is executed by the interpreter directly during the program's operation. This makes it easy to learn and convenient to use. 🚀
Why study JavaScript? 🎯
Simplicity: minimum entry threshold.
Flexibility: can be used both on the client and on the server.
Many applications: automation, data processing, scripting.
Popularity: one of the most popular programming languages.
How to install JavaScript? 💻
To work with JavaScript you will need:
✅ Installed Node.js (to run the code locally). You can download it from official website.
✅ Code editor. Recommended Visual Studio Code.
How to check if JavaScript is installed? 🛠
Open the terminal and enter:
node -vIf Node.js is installed, its version will appear. Now you can write code! 🚀
JavaScript Syntax Basics 📚
1. Variables 📦
Variables in JavaScript can be declared using let, const or var:
Example of declaring variables:
let name = "Alice"; // Line
const age = 25; // Number
let pi = 3.14; // Floating point number
console.log("Name:", name, "Age:", age, "Pi:", pi);2. Data output 📤
To output data to the console, use console.log().
Example:
console.log("Hello, world!");3. Conditional operators (if...else) 🧐
Allow the program to make decisions.
Example:
let age = 20;
if (age >= 18) {
console.log("You're an adult!");
} else {
console.log("You're still a child.");
}4. Loops (for, while) 🔄
Loops allow you to perform the same action multiple times.
Example:
for (let i = 1; i <= 5; i++) {
console.log(i);
}5. Functions 📐
Functions help to structure the code.
Example:
function square(x) {
return x * x;
}
console.log("Square of the number 5:", square(5));Simple projects in JavaScript 💡
1. Random number generator 🎲
The program generates a random number from 1 to 100.
function getRandomNumber() {
return Math.floor(Math.random() * 100) + 1;
}
console.log("Random number:", getRandomNumber());2. Password generator 🔑
The program creates a random password of 8 characters.
function generatePassword(length = 8) {
const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*";
let password = "";
for (let i = 0; i < length; i++) {
password += chars[Math.floor(Math.random() * chars.length)];
}
return password;
}
console.log("Random password:", generatePassword());3. Checking the string for palindromes 🔄
The program checks whether the string is a palindrome.
function isPalindrome(str) {
const cleanStr = str.toLowerCase().replace(/[^a-z0-9]/g, '');
return cleanStr === cleanStr.split('').reverse().join('');
}
console.log("Is 'racecar' a palindrome?", isPalindrome("racecar"));4. Counting the number of vowels in a string 🔢
The program counts how many vowels there are in the line.
function countVowels(str) {
const vowels = "aeiouAEIOU";
return str.split('').filter(char => vowels.includes(char)).length;
}
console.log("Number of vowels in 'Hello, JavaScript':", countVowels("Hello, JavaScript"));5. Fibonacci sequence 🔢
The program calculates the first 10 numbers of the Fibonacci sequence.
function fibonacci(n) {
let fib = [0, 1];
for (let i = 2; i < n; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
return fib;
}
console.log("The first 10 Fibonacci numbers:", fibonacci(10));Conclusion 🎉
JavaScript is a powerful and convenient programming language that is easy to learn. We have analyzed its basics: variables, input/output, conditions, loops and functions. Now you can write your first programs! 🚀
The more you practice, the better your code becomes. Experiment, try new tasks and master programming! Good luck learning JavaScript! 😊
