JavaScript concepts

Node.js is a popular platform for developing server-side applications. It's built on top of JavaScript, so having a strong foundation in the language is essential for working with Node.js.

In this article, we'll cover the key JavaScript concepts you need to know before getting started with Node.js.

Variables:

Variables are used to store data in a program. In JavaScript, we can declare variables using the 'var', 'let', or 'const' keywords. The 'var' keyword is used to declare a variable that can be reassigned. The 'let' keyword is used to declare a variable that can be reassigned within a block of code. The 'const' keyword is used to declare a variable that can't be reassigned.

var name = 'John';
let age = 30;
const city = 'New York';

name = 'Jane'; // valid
age = 31; // valid
city = 'London'; // invalid

Data Types:

JavaScript has several built-in data types, including number, string, boolean, null, and undefined.

let num = 10;
let str = 'hello';
let bool = true;
let n = null;
let u;

console.log(typeof num); // number
console.log(typeof str); // string
console.log(typeof bool); // boolean
console.log(typeof n); // object
console.log(typeof u); // undefined

Arrays:

Arrays are used to store a collection of data. We can access the elements of an array using their index.

let fruits = ['apple', 'banana', 'cherry'];

console.log(fruits[0]); // apple

Loops:

Loops are used to repeat a block of code a certain number of times. The two most commonly used loops in JavaScript are for and forEach.

let numbers = [1, 2, 3, 4, 5];

// using for loop
for (let i = 0; i < numbers.length; i++) {
  console.log(numbers[i]);
}

// using forEach loop
numbers.forEach(function(num) {
  console.log(num);
});

Functions:

Functions are reusable blocks of code that perform a specific task. Functions in JavaScript are declared using the 'function' keyword.

function sayHello(name) {
  console.log('Hello, ' + name + '!');
}

sayHello('John'); // Hello, John!

Objects:

Objects are used to store data in key-value pairs.

let person = {
  name: 'John',
  age: 30,
  city: 'New York'
};

console.log(person.name); // John

Last updated