Feb 15, '25 02:00

Basics of JavaScript: variables, functions, scopes

JavaScript is one of the most popular programming languages for web development. To work with it effectively, it is important to understand the basic concepts: variables, functions, and scopes. These concepts define how data is stored and passed in a progra...

Read post
Share
🔥 More posts
This content has been automatically translated from Ukrainian.

JavaScript is one of the most popular programming languages for web development. To work with it effectively, it is important to understand the basic concepts: variables, functions, and scopes. These concepts define how data is stored and passed in a program, as well as which parts of the code have access to certain variables.

Variables in JavaScript

In JavaScript, there are three ways to declare a variable: var, let, and const. Each of them has its own characteristics.

var oldVariable = "This is an outdated way to declare variables";
let modernVariable = "Recommended for variables that change (heh heh heh)";
const constantVariable = "This is a constant, it cannot be redefined";

var has function scope and is subject to hoisting, which can cause unexpected errors:

console.log(value); // undefined
var value = 10;

The variable value exists in memory before the code is executed, but has no value. This creates potential problems, which is why var is rarely used.

let has block scope, making it better suited for modern code:

if (true) {
  let number = 42;
}
console.log(number); // Error: number is not defined

const is used for values that do not change:

const PI = 3.14;
PI = 3.1415; // Error: assigning new values is not possible

Functions in JavaScript

Functions allow you to structure code and make it reusable. There are several ways to declare them:

function classicFunction() {
  return "This is a classic function";
}

const arrowFunction = () => "This is an arrow function";

Functions can accept arguments and return values:

function add(a, b) {
  return a + b;
}
console.log(add(2, 3)); // 5

Arrow functions are often used in modern JavaScript due to their shorter syntax:

const multiply = (a, b) => a * b;
console.log(multiply(4, 5)); // 20

🔥 More posts

All posts
Feb 17, '25 02:00

What is SASS?

SASS is a preprocessor for CSS that allows you to write styled code faster and more efficiently. SASS ...