Sign In
Sign In

How to Use JavaScript Array map()

How to Use JavaScript Array map()
Emmanuel Oyibo
Technical writer
JavaScript
16.10.2024
Reading time: 12 min

Arrays play a crucial role in JavaScript programming. They let you store and manipulate collections of data with ease. One powerful method for working with arrays is the map() function. The map() method creates a new array by applying a given function to each element of the original array.

Simply put, map() takes your array, processes each item through a function you provide, and returns a new array with the transformed items. It doesn't change the original array, which helps prevent unintended side effects in your code. 

In this guide, we'll explore the map() function, how it works, and how you can use it in your JavaScript projects.

Basic Syntax and Parameters of map()

The map() function is a built-in method in JavaScript that you can use on arrays. It creates a new array by applying a function to each element of the original array.

Below is the syntax:

let newArray = originalArray.map(function(currentValue, index, array) {
  // return element to newArray
});

Or, using arrow function syntax:

let newArray = originalArray.map((currentValue, index, array) => {
  // return element to newArray
});

Let's break down the parameters:

  • currentValue: The current element being processed in the array.

  • index (optional): The index of the current element.

  • array (optional): The original array to which map() is being applied.

However, you must provide a callback function that tells map() how to transform each element. This function runs once for every item in the array. Importantly, map() does not modify the original array; instead, it returns a new array with the transformed elements.

For example, if you have an array of numbers and want to add 1 to each number, you can use map() like this:

let numbers = [1, 2, 3, 4, 5];
let incrementedNumbers = numbers.map(number => number + 1);
console.log(incrementedNumbers);

Output:

[ 2, 3, 4, 5, 6 ]

In this example, the map() function takes each number in the numbers array, adds 1 to it, and returns a new array called incrementedNumbers.

Using map() to Transform Array Elements

The map() method is helpful when you need to transform each element in an array. It applies a function to every item and returns a new array with the transformed elements. Let's explore how you can use map() to manipulate array data.

Let’s say you have an array of numbers, and want to double each number. Instead of writing a loop, you can achieve this with map() in a more concise way.

let numbers = [1, 2, 3, 4, 5];
let doubled = numbers.map(number => number * 2);
console.log(doubled);

Output:

[ 2, 4, 6, 8, 10 ]

In this example, numbers.map(number => number * 2) takes each element in the numbers array, multiplies it by two, and stores the result in a new array called doubled. The original numbers array remains unchanged.

You can also transform arrays of strings. Let's say you have an array of names and you want to convert them all to uppercase letters.

let names = ['alice', 'bob', 'charlie'];
let upperNames = names.map(name => name.toUpperCase());
console.log(upperNames);

Output:

[ 'ALICE', 'BOB', 'CHARLIE' ]

Here, map() applies the toUpperCase() method to each string in the names array, resulting in a new array upperNames with all names in uppercase.

Another practical use is when working with arrays of objects. For example, you might have an array of user objects and want to extract a specific property from each one.

let users = [
  { name: 'Alice', age: 25 },
  { name: 'Bob', age: 30 },
  { name: 'Charlie', age: 35 }
];

let userNames = users.map(user => user.name);
console.log(userNames);

Output:

[ 'Alice', 'Bob', 'Charlie' ]

In this case, map() extracts the name property from each user object and creates a new array userNames.

Practical Examples of map()

The map() function really shines when working with real-world data. Let's see some practical examples to see how map() can simplify your JavaScript code.

Example 1: Converting Temperatures

Let’s assume you have an array of temperatures in Celsius and want to convert them to Fahrenheit. You can use map() to apply the conversion formula to each element.

let celsius = [0, 20, 30, 40];
let fahrenheit = celsius.map(temp => temp * 9/5 + 32);
console.log(fahrenheit);

Output:

[ 32, 68, 86, 104 ]

In this example, map() takes each temperature in the celsius array, converts it to Fahrenheit, and returns a new array called fahrenheit.

Example 2: Extracting Usernames from Emails

Imagine you have an array of email addresses and you want to extract the usernames.

let emails = ['[email protected]', '[email protected]', '[email protected]'];
let usernames = emails.map(email => email.split('@')[0]);
console.log(usernames);

Output:

[ 'alice', 'bob', 'charlie' ]

Here, map() processes each email address, splits it at the @ symbol, and extracts the username part.

Example 3: Applying Discounts to Products

If you have an array of product objects and need to apply a discount to each product's price, map() makes this task easy.

let products = [
  { name: 'Laptop', price: 1000 },
  { name: 'Phone', price: 500 },
  { name: 'Tablet', price: 750 }
];

let discountedProducts = products.map(product => {
  return {
    name: product.name,
    price: product.price * 0.9 // Apply a 10% discount
  };
});

console.log(discountedProducts);

Output:

[
  { name: 'Laptop', price: 900 },
  { name: 'Phone', price: 450 },
  { name: 'Tablet', price: 675 }
]

In this case, map() creates a new array discountedProducts with the discounted prices, leaving the original products array unchanged.

Example 4: Formatting Dates

If you have an array of date strings and want to format them into a more readable form.

let dates = ['2023-01-01', '2023-02-14', '2023-12-25'];
let formattedDates = dates.map(date => {
  let options = { year: 'numeric', month: 'long', day: 'numeric' };
  return new Date(date).toLocaleDateString(undefined, options);
});

console.log(formattedDates);

Output:

[ 'January 1, 2023', 'February 14, 2023', 'December 25, 2023' ]

Here, map() converts each date string into a Date object and formats it using toLocaleDateString().

Example 5: Generating HTML Elements

If you're working with the DOM, you can use map() to create an array of HTML elements.

let fruits = ['apple', 'banana', 'cherry'];
let fruitListItems = fruits.map(fruit => `<li>${fruit}</li>`);
console.log(fruitListItems);

Output:

[ '<li>apple</li>', '<li>banana</li>', '<li>cherry</li>' ]

You can then join these items and insert them into your HTML.

let fruits = ['apple', 'banana', 'cherry'];
let fruitListItems = fruits.map(fruit => `<li>${fruit}</li>`);

let fruitList = `<ul>${fruitListItems.join('')}</ul>`;
console.log(fruitList);

Output:

<ul><li>apple</li><li>banana</li><li>cherry</li></ul>

This demonstrates how map() can help generate dynamic content.

Chaining map() with Other Array Methods

The map() function becomes even more powerful when you chain it with other array methods like filter(), reduce(), or sort(). You can perform complex data transformations easily when you combine these methods.

Filtering and Mapping

Let’s say you have an array of numbers and you want to double only the even numbers.

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

let doubledEvens = numbers
  .filter(number => number % 2 === 0)
  .map(number => number * 2);

console.log(doubledEvens);

Output:

[ 4, 8, 12 ]

In this example, filter() first selects the even numbers from the numbers array. Then, map() doubles each of those numbers.

Mapping and Reducing

Imagine you have an array of objects representing products and want to calculate the total price of all products after applying a discount.

let products = [
  { name: 'Laptop', price: 1000 },
  { name: 'Phone', price: 500 },
  { name: 'Tablet', price: 750 }
];

let totalDiscountedPrice = products
  .map(product => product.price * 0.9) // Apply a 10% discount
  .reduce((total, price) => total + price, 0);

console.log(totalDiscountedPrice);

Output:

2025

Here, map() creates a new array with the discounted prices. Then, reduce() sums up the prices to get the total.

Sorting Mapped Values

Imagine you have an array of strings and want to convert them to uppercase and then sort them alphabetically.

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

let sortedFruits = fruits
  .map(fruit => fruit.toUpperCase())
  .sort();

console.log(sortedFruits);

Output:

[ 'APPLE', 'BANANA', 'CHERRY' ]

In this case, map() transforms each fruit name to uppercase, and sort() arranges them alphabetically.

Complex Data Transformation

Let’s assume you have an array of user objects, and you want to get a sorted list of active users' usernames.

let users = [
  { username: 'alice', active: true },
  { username: 'bob', active: false },
  { username: 'charlie', active: true }
];

let activeUsernames = users
  .filter(user => user.active)
  .map(user => user.username)
  .sort();

console.log(activeUsernames);

Output:

[ 'alice', 'charlie' ]

Here, you filter out inactive users, extract their usernames with map(), and sort the usernames alphabetically.

Handling Edge Cases with map()

While the map() function is powerful, handling edge cases is important to prevent unexpected behavior. Let's explore some common scenarios and how to manage them.

Dealing with Empty Arrays

If you apply map() to an empty array, it simply returns another empty array. There's no need to add extra checks for this case.

let emptyArray = [];
let result = emptyArray.map(item => item * 2);
console.log(result);

Output:

[ ]

Handling Undefined or Null Elements

When your array contains undefined or null values, map() will pass them to your callback function. You need to ensure your function can handle these values without throwing errors.

let values = [1, null, 3, undefined, 5];
let processedValues = values.map(value => {
  if (value === null || value === undefined) {
    return 0; // Assign a default value
  }
  return value * 2;
});
console.log(processedValues);

Output:

[ 2, 0, 6, 0, 10 ]

In this example, we check for null or undefined and assign a default value of 0 before performing the calculation.

Avoiding Holes in Arrays

Arrays can have holes if elements are deleted or uninitialized. The map() function skips these holes, which might lead to unexpected results.

let sparseArray = [1, , 3, , 5]; // Note the missing elements
let doubled = sparseArray.map(number => number * 2);
console.log(doubled);

Output:

[ 2, <1 empty item>, 6, <1 empty item>, 10 ]

To handle this, you can use the Array.from() method to create an array without holes before mapping.

let sparseArray = [1, , 3, , 5]; // Note the missing elements
let doubled = sparseArray.map(number => number * 2);

let denseArray = Array.from(sparseArray);
let doubledDense = denseArray.map(number => number * 2);
console.log(doubledDense);

Output:

[ 2, NaN, 6, NaN, 10 ]

Now, the missing elements are converted to undefined, and you can handle them accordingly.

Managing Asynchronous Operations

The map() function doesn't support asynchronous operations natively. To perform asynchronous tasks, consider using Promise.all() with map().

let urls = ['url1', 'url2', 'url3'];

let fetchPromises = urls.map(url => fetch(url));

Promise.all(fetchPromises)
  .then(responses => {
    // Handle responses
  })
  .catch(error => {
    // Handle errors
  });

Here, map() creates an array of promises, and Promise.all() waits for all of them to resolve.

Using map() with Callback Functions

The map() method relies on a callback function to process each element in an array. Let's explore different ways to use map() with callback functions.

Using Named Functions

You can pass a named function to map() for better readability, especially when the transformation logic is complex.

function doubleNumber(number) {
  return number * 2;
}

let numbers = [1, 2, 3, 4, 5];
let doubledNumbers = numbers.map(doubleNumber);

console.log(doubledNumbers);

Output:

[ 2, 4, 6, 8, 10 ]

In this example, the doubleNumber function is passed as the callback to map(). Each element in the numbers array is doubled, resulting in a new array doubledNumbers.

Using Anonymous Functions

Anonymous functions can be used directly within map() for simple transformations.

let numbers = [1, 2, 3, 4, 5];
let squaredNumbers = numbers.map(function(number) {
  return number * number;
});

console.log(squaredNumbers);

Output:

[ 1, 4, 9, 16, 25 ]

Here, an anonymous function computes the square of each number in the array.

Using Arrow Functions

Arrow functions offer a concise syntax and are commonly used with map().

let numbers = [1, 2, 3, 4, 5];
let tripledNumbers = numbers.map(number => number * 3);

console.log(tripledNumbers);

Output:

[ 3, 6, 9, 12, 15 ]

The arrow function number => number * 3 triples each number in the array.

Using Callback Functions with Multiple Parameters

The callback function can accept additional parameters like index and array, providing more context during the mapping process.

let numbers = [10, 20, 30, 40, 50];
let adjustedNumbers = numbers.map((number, index) => number - index * 2);

console.log(adjustedNumbers);

Output:

[ 10, 18, 26, 34, 42 ]

In this example, each number is adjusted based on its index in the array.

Using thisArg with map()

The map() method accepts an optional second argument called thisArg, which sets the value of this inside the callback function.

let multiplier = {
  factor: 2
};

let numbers = [1, 2, 3];
let scaledNumbers = numbers.map(function(number) {
  return number * this.factor;
}, multiplier);

console.log(scaledNumbers);

Output:

[ 2, 4, 6 ]

By passing multiplier as the thisArg, the callback function can access this.factor to scale each number.

Performance Considerations for map()

When using map(), it's important to consider performance. Since map() creates a new array, it consumes additional memory. Therefore, with very large arrays, this extra memory usage can affect your application's efficiency.

Furthermore, while map() offers clean and readable code, traditional loops like for or while loops might execute faster in performance-critical situations. If speed is crucial, consider using a loop instead of map().

Moreover, keep your callback functions simple. Complex operations inside the map() callback can slow down processing. To maintain optimal performance, aim for straightforward transformations.

Finally, use map() only when you need the new array it returns. If you're performing actions without needing the resulting array, methods like .forEach() are more appropriate. This avoids unnecessary memory allocation and enhances performance.

Conclusion

The JavaScript map() function is a powerful tool for array manipulation. By applying a function to each element, it creates a new array with transformed values, making tasks like data conversion and extraction straightforward. Understanding how to use map() effectively allows you to write cleaner, more efficient code.

JavaScript
16.10.2024
Reading time: 12 min

Similar

Microservices

Database Connection in Python, Go, and JavaScript

Databases are an essential part of almost any project today. Database interactions are especially familiar to system and database administrators, DevOps/SRE professionals, and software developers. While administrators typically deploy one or multiple database instances and configure the necessary connection parameters for applications, developers need to connect directly to the database within their code. This article explores how to connect to databases using different programming languages. Prerequisites We will provide examples for connecting to MySQL, PostgreSQL, Redis, MongoDB, and ClickHouse databases using Python, Go, and JavaScript. To follow this guide, you will need: A database deployed on a server or in the cloud. Installed environments for Python, Go, and JavaScript, depending on your application programming language. Additionally for Python: pip installed. Additionally for JavaScript: Node.js and npm installed. Database Connection in Python MySQL and Python For connecting to MySQL databases, we can use a Python driver called MySQL Connector. Install the driver using pip: pip install mysql-connector-python Initialize a new connection: Import the mysql.connector library and the Error class to handle specific connection errors. Create a function named create_connection, passing the database address (host), user name (user), and user password (password). To establish the connection, define a class called create_connection that receives the variable names containing the database connection details. import mysql.connector from mysql.connector import Error def create_connection(host_name, user_name, user_password): connection = None try: connection = mysql.connector.connect( host="91.206.179.29", user="gen_user", password="m-EE6Wm}z@wCKe" ) print("Successfully connected to MySQL Server!") except Error as e: print(f"The error '{e}' occurred") return connection def execute_query(connection, query): cursor = connection.cursor() try: cursor.execute(query) connection.commit() print("Query executed successfully") except Error as e: print(f"The error '{e}' occurred") connection = create_connection("91.206.179.29", "gen_user", "m-EE6Wm}z@wCKe") Run the script. If everything works correctly, you will see the "Successfully connected to MySQL Server!" message. If any errors occur, the console will display error code and description. Create a new table: Connect to the database using the connection.database class, specifying the name of the database. Note that the database should already exist. To create a table, initialize a variable create_table_query containing the SQL CREATE TABLE query. For data insertion, initialize another variable insert_data_query with the SQL INSERT INTO query. To execute each query, use the execute_query class, which takes the database connection string and the variable containing the SQL query. connection.database = 'test_db' create_table_query = """ CREATE TABLE IF NOT EXISTS users ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, age INT NOT NULL ) """ execute_query(connection, create_table_query) insert_data_query = """ INSERT INTO users (name, age) VALUES ('Alice', 30), ('Bob', 25) """ execute_query(connection, insert_data_query) if connection.is_connected(): connection.close() print("Connection closed") Run the script. PostgreSQL and Python Python offers several plugins for connecting to PostgreSQL, but the most popular one is psycopg2, which we will use here. Psycopg2 is one of the most frequently used Python plugins for PostgreSQL connections. One of its key advantages is its support for multithreading which allows you to maintain the database connection across multiple threads. Install psycopg2 using pip (if not already installed): pip install psycopg2-binary Connect to PostgreSQL. Import the Python psycopg2 package and create a function create_new_conn, using the try block. Establish the connection with the psycopg2.connect function, which requires the database name, user name, password, and database address as input. To initialize the connection, use the create_new_conn() function. Here’s the full code example for connecting to a database: import psycopg2 from psycopg2 import OperationalError def create_new_conn(): conn_to_postgres = None while not conn_to_postgres: try: conn_to_postgres = psycopg2.connect( default_db="default_db", default_user="gen_user", password_for_default_user="PasswordForDefautUser9893#", db_address="91.206.179.128" ) print("The connection to PostgreSQL has been successfully established!") except OperationalError as e: print(e) return conn_to_postgres conn_to_postgres = create_new_conn() Run the script: python3 connect_to_postgres.py If successful, you will see the "The connection to PostgreSQL has been successfully established!" message. . Next, create a table named books, which will have three columns. Use the cursor class for SQL expressions, such as creating database objects. If the query involves adding or modifying data, you must call the conn_to_postgres.commit() function afterward to apply the changes. import psycopg2 from psycopg2 import OperationalError def create_new_conn(): conn_to_postgres = None while not conn_to_postgres: try: conn_to_postgres = psycopg2.connect( default_db="default_db", default_user="gen_user", password_for_default_user="PasswordForDefautUser9893#", db_address="91.206.179.128" ) except OperationalError as e: print(e) return conn_to_postgres conn_to_postgres = create_new_conn() cursor = conn_to_postgres.cursor() cursor.execute(""" CREATE TABLE books ( book_id INT PRIMARY KEY NOT NULL, book_name VARCHAR(255) NOT NULL, book_author VARCHAR(255) NOT NULL ) """) conn_to_postgres.commit() print("Table Created successfully") Run the script: python3 create_table.py Now, let’s run INSERT INTO to add a new line: cursor.execute(""" INSERT INTO books (book_id,book_name,book_author) VALUES (1, 'Long Walk to Freedom', 'Nelson_Mandela') """) The full code is below: import psycopg2 from psycopg2 import OperationalError def create_new_conn(): conn_to_postgres = None while not conn_to_postgres: try: conn_to_postgres = psycopg2.connect( default_db="default_db", default_user="gen_user", password_for_default_user="PasswordForDefautUser9893#", db_address="91.206.179.128" ) except OperationalError as e: print(e) return conn_to_postgres conn_to_postgres = create_new_conn() cursor = conn_to_postgres.cursor() cursor.execute(""" INSERT INTO books (book_id,book_name,book_author) VALUES (1, 'Long Walk to Freedom', 'Nelson_Mandela') """) conn_to_postgres.commit() conn_to_postgres.close() print("Data inserted successfully") Run the script: python3 insert-data.py Redis and Python Redis belongs to the class of NoSQL databases, where data is stored in memory rather than on hard drives. It uses a key-value format for data storage. Redis has a wide range of applications, from data storage and caching to serving as a message broker. We will use the redis-py (or simply redis) library for connecting to Redis. Install the Redis library using pip: pip install redis Connecting to a Redis instance: Use a try block structure for connection, specifying the function redis.StrictRedis where you provide the Redis address, port, and user password. import redis try: connect_to_redis_server = redis.StrictRedis( redis_db_host=91.206.179.128, redis_db_port=6379, redis_user_password='PasswordForRedis6379') print connect_to_redis_server connect_to_redis_server.ping() print 'Successfully connected to Redis Server!' except Exception as ex: print 'Error:', ex exit('Failed to connect to Redis server.') Run the script: python3 connect_to_redis.py If successful, you will see a message like "Successfully connected to Redis Server!". Unlike relational databases, Redis stores data in a key-value format. The key uniquely identifies the corresponding value. Use the set method to create a new record. The example below creates a record with the key City and the value Berlin: print('Create new record:', connect_to_redis_server.set("City", "Berlin")) Use the get method to retrieve the value associated with a key: print('Print record using record key:', connect_to_redis_server.get("City")) Use the delete method to remove a record by its key: print('Delete record with key:', connect_to_redis_server.delete("City")) The complete code fragment is below. import redis try: connect_to_redis_server = redis.StrictRedis( redis_db_host=91.206.179.128, redis_db_port=6379, redis_user_password='PasswordForRedis6379') print ('New record created:', connect_to_redis_server.set("City", "Berlin")) print ('Print created record using record key', connect_to_redis_server.get("City")) print ('Delete created record with key :', connect_to_redis_server.delete("City")) except Exception as ex: print ('Error:', ex) MongoDB and Python MongoDB is another widely used NoSQL database that belongs to the document-oriented category. Data is organized as JSON-like documents. To connect to a MongoDB database with Python, the recommended library is PyMongo, which provides a synchronous API. Install the PyMongo plugin: pip3 install pymongo Connect to MongoDB server using the following Python code. Import the pymongo module and use the MongoClient class to specify the database server address. To establish a connection to the MongoDB server, use a try block for error handling: import pymongo connect_to_mongo = pymongo.MongoClient("mongodb://91.206.179.29:27017/") first_db = connect_to_mongo["mongo-db1"] try: first_db.command("serverStatus") except Exception as e: print(e) else: print("Successfully connected to MongoDB Server!") connect_to_mongo.close() Run: python3 connect_mongodb.py If the connection is successfully established, the script will return the message: "Successfully connected to MongoDB Server!" Add data to MongoDB. To add data, you need to create a dictionary. Let's create a dictionary named record1, containing three keys: record1 = { "name": "Alex", "age": 25, "location": "London" } To insert the dictionary data, use the insert_one method in MongoDB. insertrecord = collection1.insert_one(record1) import pymongo connect_to_mongo = pymongo.MongoClient("mongodb://91.206.179.29:27017/") db1 = connect_to_mongo["newdb"] collection1 = db1["userdata"] record1 = { "name": "Alex", "age": 25, "location": "London" } insertrecord = collection1.insert_one(record1) print(insertrecord) Run the script: python3 connect_mongodb.py ClickHouse and Python ClickHouse is a columnar NoSQL database where data is stored in columns rather than rows. It is widely used for handling analytical queries. Install the ClickHouse driver for Python. There is a dedicated plugin for ClickHouse called clickhouse-driver. Install the driver using the pip package manager: pip install clickhouse-driver Connect to ClickHouse. To initialize a connection with ClickHouse, you need to import the Client class from the clickhouse_driver library. To execute SQL queries, use the client.execute function. You also need to specify the engine. For more details on supported engines in ClickHouse, you can refer to the official documentation. We'll use the default engine, MergeTree. Next, create a new table called users and insert two columns with data. To list the data to be added to the table, use the tuple data type. After executing the necessary queries, make sure to close the connection to the database using the client.disconnect() method. The final code will look like this: from clickhouse_driver import Client client = Client(host=91.206.179.128', user='root', password='P@$$w0rd123', port=9000) client.execute(''' CREATE TABLE IF NOT EXISTS Users ( id UInt32, name String, ) ENGINE = MergeTree() ORDER BY id ''') data = [ (1, 'Alice'), (2, 'Mary') ] client.execute('INSERT INTO Users (id, name) VALUES', data) result = client.execute('SELECT * FROM Users') for row in result: print(row) client.disconnect() Database Connection in Go Go is one of the youngest programming languages, developed in 2009 by Google.  It is widely used in developing microservice architectures and network utilities. For example, services like Docker and Kubernetes are written in Go. Go supports integrating all popular databases, including PostgreSQL, Redis, MongoDB, MySQL, ClickHouse, etc. MySQL and Go For working with the MySQL databases in Go, use the go-sql-driver/mysql driver. Create a new directory for storing project files and navigate into it: mkdir mysql-connect && cd mysql-connect Create a go.mod file to store the dependencies: go mod init golang-connect-mysql Download the MySQL driver using the go get command: go get -u github.com/go-sql-driver/mysql Create a new file named main.go. Specify the database connection details in the dsn variable: package main import ( "database/sql" "fmt" "log" _ "github.com/go-sql-driver/mysql" ) func main() { dsn := "root:password@tcp(localhost:3306)/testdb" db, err := sql.Open("mysql", dsn) if err != nil { log.Fatal(err) } defer db.Close() if err := db.Ping(); err != nil { log.Fatal(err) } fmt.Println("Successfully connected to the database!") query := "INSERT INTO users (name, age) VALUES (?, ?)" result, err := db.Exec(query, "Alex", 25) if err != nil { log.Fatal(err) } lastInsertID, err := result.LastInsertId() if err != nil { log.Fatal(err) } fmt.Printf("Inserted data with ID: %d\n", lastInsertID) } PostgreSQL and Go To connect to PostgreSQL, use the pq driver. Before installing the driver, let's prepare our environment. Create a new directory for storing the project files and navigate into it: mkdir postgres-connect && cd postgres-connect Since we will be working with dependencies, we need to create a go.mod file to store them: go mod init golang-connect-postgres Download the pq driver using the go get command: go get github.com/lib/pq Create a new file named main.go. In addition to importing the pq library, it is necessary to add the database/sql library as Go does not come with official database drivers by default. The database/sql library consists of general, independent interfaces for working with databases. It is also important to note the underscore (empty identifier) when importing the pq module: _ "github.com/lib/pq" The empty identifier is used to avoid the "unused import" error, as in this case, we only need the driver to be registered in database/sql. The fmt package is required to output data to the standard output stream, for example, to the console. To open a connection to the database, the sql.Open function is used, which takes the connection string (connStr) and the driver name (postgres). The connection string specifies the username, database name, password, and host address: package main import ( "database/sql" "fmt" "log" _ "github.com/lib/pq" ) func main() { connStr := "user=golang dbname=db_for_golang password=Golanguserfordb0206$ host=47.45.249.146 sslmode=disable" db, err := sql.Open("postgres", connStr) if err != nil { log.Fatal(err) } defer db.Close() err = db.Ping() if err != nil { log.Fatal(err) } fmt.Println("Successfully connected to PostgreSQL!") } Compile and run: go run main.go If everything works correctly, the terminal will display the message Successfully connected to PostgreSQL! Now, let's look at an example of how to insert data into a table.  First, we need to create a table in the database. When using Hostman cloud databases, you can copy the PostgreSQL connection string displayed in the "Connections" section of the Hostman web interface. Make sure that the postgresql-client utility is installed on your device beforehand. Enter the psql shell and connect to the previously created database: \c db_for_golang Create a table named Cities with three fields — city_id, city_name, and city_population: CREATE TABLE Cities ( city_id INT PRIMARY KEY, city_name VARCHAR(45) NOT NULL, city_population INT NOT NULL); Grant full privileges to the created table for the user: GRANT ALL PRIVILEGES ON TABLE cities TO golang; The function db.Prepare is used to prepare data. It specifies the query for insertion in advance. To insert data, use the function stmt.Exec. In Go, it's common to use plain SQL without using the ORM (Object-Relational Mapping) approach. stmt, err := db.Prepare("INSERT INTO Cities(city_id, city_name, city_population) VALUES($1, $2, $3)") if err != nil { log.Fatal(err) } defer stmt.Close() _, err = stmt.Exec(1, "Toronto", 279435) if err != nil { log.Fatal(err) } fmt.Println("Data inserted successfully!") } If all works correctly, you will see: Data inserted successfully! Redis and Go To connect to Redis, you need to use the go-redis driver. Сreate a new directory: mkdir connect-to-redis && cd connect-to-redis Prepare the dependency file: go mod init golang-connect-redis And optimize them: go mod tidy Download the go-redis module: go get github.com/go-redis/redis/v8 To connect to Redis, use the redis.Options function to specify the address and port of the Redis server. Since Redis does not use authentication by default, you can leave the Password field empty and use the default database (database 0): package main import ( "context" "fmt" "log" "github.com/go-redis/redis/v8" ) func main() { rdb := redis.NewClient(&redis.Options{ Addr: "91.206.179.128:6379", Password: "", DB: 0, }) ctx := context.Background() _, err := rdb.Ping(ctx).Result() if err != nil { log.Fatalf("Couldn't connect to Redis: %v", err) } fmt.Println("Successfully connected to Redis!") } You should see the message «Successfully connected to Redis!» MongoDB and Go To work with MongoDB, we'll use the mongo driver. Create a new directory to store the project structure: mkdir connect-to-mongodb && cd connect-to-mongodb Initialize the dependency file: go mod init golang-connect-mongodb Download the mongo library: go get go.mongodb.org/mongo-driver/mongo Connect to MongoDB using the options.Client().ApplyURI method. It takes a connection string such as mongodb://91.206.179.29:27017, where 91.206.179.29 is the MongoDB server address and 27017 is the port for connecting to MongoDB. The options.Client().ApplyURI string is used only for specifying connection data. To check the connection status, you can use another function, client.Ping, which shows the success or failure of the connection: package main import ( "context" "fmt" "log" "time" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) func main() { clientOptions := options.Client().ApplyURI("mongodb://91.206.179.29:27017") client, err := mongo.Connect(context.TODO(), clientOptions) if err != nil { log.Fatalf("Couldn't connect to MongoDB server: %v", err) } fmt.Println("successfully connected to MongoDB!") ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() err = client.Ping(ctx, nil) if err != nil { log.Fatalf("Could not ping MongoDB server: %v", err) } fmt.Println("Ping MongoDB server successfully!") } You should see the message: successfully connected to MongoDB!Ping MongoDB server successfully MongoDB uses collections to store data. You can create collections using the .Collection function.  Below, we will create a database called first-database and a collection called first-collection. The collection will have a new document, containing three keys: user-name, user-age, and user-email. collection := client.Database("first-database").Collection("first-collection") document := map[string]interface{}{ "user-name": "Alice", "user-age": 25, "user-email": "[email protected]", } insertResult, err := collection.InsertOne(ctx, document) if err != nil { log.Fatalf("Couldn't insert new document: %v", err) } fmt.Printf("Inserted new document with ID: %v\n", insertResult.InsertedID) if err := client.Disconnect(ctx); err != nil { log.Fatalf("Could not disconnect from MongoDB: %v", err) } fmt.Println("Disconnected from MongoDB!") } If successful, you will see the Inserted new document message with the document ID.  ClickHouse and Go To work with ClickHouse, use the clickhouse-go driver. Create a new directory to store the project files and navigate to it: clickhouse-connect && cd clickhouse-connect Create a go.mod file to store the dependencies: go mod init golang-connect-clickhouse Download the Clickhouse driver using the command: go get github.com/ClickHouse/clickhouse-go/v2 Create a new file named main.go, where you will specify the connection data to ClickHouse. package main import ( "database/sql" "log" "github.com/ClickHouse/clickhouse-go/v2" ) func main() { dsn := "tcp://localhost:9000?username=user1&password=PasswordForuser175465&database=new_db" db, err := sql.Open("clickhouse", dsn) if err != nil { log.Fatal(err) } defer db.Close() if err := db.Ping(); err != nil { log.Fatal(err) } log.Println("Connected to ClickHouse!") } Database Connection in JavaScript In JavaScript, all connections to external services are made using the Node.js platform. Make sure that you have Node.js and the npm package manager installed on your device. MySQL and JavaScript To work with MySQL, use the mysql2 driver. Create a directory where we will store the project files: mkdir js-mysql-connect && cd js-mysql-connect Initialize the project: npm init -y Install the mysql2 library: npm install mysql2 Use the following code to connect to MySQL: const mysql = require('mysql2'); const connection_to_mysql = mysql.createConnection({ host: 'localhost', user: 'root', password: 'PasswordForRoot74463', database: db1, }); connection_to_mysql.connect((err) => { if (err) { console.error('Error connecting to MySQL:', err.message); return; } console.log('Successfully connected to MySQL Server!'); connection_to_mysql.end((endErr) => { if (endErr) { console.error('Error closing the connection_to_mysql:', endErr.message); } else { console.log('Connection closed.'); } }); }); PostgreSQL and JavaScript Connecting to PostgreSQL is done using the pg library. Create a directory where we will store the project files: mkdir js-postgres-connect && cd js-postgres-connect Initialize the project: npm init -y Install the pg library: npm install pg To connect to PostgreSQL, first import the pg library. Then, create a constant where you specify variables for the database address, username, password, database name, and port. Use the new pg.Client class to pass the connection data. We will create a table called cities and add two records into it. To do this, we will use the queryDatabase function, which contains the SQL queries. const pg = require('pg'); const config = { postgresql_server_host: '91.206.179.29', postgresql_user: 'gen_user', postgresql_user_password: 'PasswordForGenUser56467$', postgresql_database_name: 'default_db', postgresql_database_port: 5432, }; const client = new pg.Client(config); client.connect(err => { if (err) throw err; else { queryDatabase(); } }); function queryDatabase() { const query = ` DROP TABLE IF EXISTS cities; CREATE TABLE cities (id serial PRIMARY KEY, name VARCHAR(80), population INTEGER); INSERT INTO cities (name, population) VALUES ('Berlin', 3645000); INSERT INTO cities (name, population) VALUES ('Paris', 2161000); `; client .query(query) .then(() => { console.log('Table created successfully!'); client.end(console.log('Closed client connection')); }) .catch(err => console.log(err)) .then(() => { console.log('Finished execution, exiting now'); process.exit(); }); } Use this command to run the code: node connect-to-postgres.js Redis and JavaScript To work with Redis, use the ioredis library. Create a directory to store the project files: mkdir js-redis-connect && cd js-redis-connect Initialize the project: npm init -y Install the ioredis library: npm install ioredis To connect to Redis, import the ioredis library. Then create a constant named redis and specify the Redis server address. Inserting data, i.e., creating key-value objects, is done using an asynchronous function named setData, which takes two values — key and value, corresponding to the data format of the Redis system. const Redis = require('ioredis'); const redis = new Redis({ host: '91.206.179.29', port: 6379, password: 'UY+p8e?Kxmqqfa', }); async function setData(key, value) { try { await redis.set(key, value); console.log('Data successfully set'); } catch (error) { console.error('Error setting data:', error); } } async function getData(key) { try { const value = await redis.get(key); console.log('Data retrieved'); return value; } catch (error) { console.error('Error getting data:', error); } } (async () => { await redis.select(1); await setData('user', 'alex'); await getData('user'); redis.disconnect(); })(); Run: node connect-to-redis.js MongoDB and JavaScript To work with MongoDB, use the mongodb driver. Create a directory for storing the project files: mkdir js-mongodb-connect && cd js-mongodb-connect Initialize the project: npm init -y Install the mongodb library: npm install mongodb To connect to MongoDB, import the mongodb library. Specify the database address in the constant uri and pass the address into the MongoClient class. const { MongoClient } = require('mongodb'); const uri = "mongodb://91.206.179.29:27017"; const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true }); async function connectToDatabase() { try { await client.connect(); console.log("Successfully connected to MongoDB!"); const database = client.db("myDatabase"); const collection = database.collection("myCollection"); const documents = await collection.find({}).toArray(); console.log("Documents found:", documents); } catch (error) { console.error("Error connecting to MongoDB:", error); } finally { await client.close(); console.log("Connection closed."); } } connectToDatabase(); ClickHouse and JavaScript To work with ClickHouse, use the clickhouse/client driver. Create a directory where we will store the project files: mkdir js-clickhouse-connect && cd js-clickhouse-connect Initialize the project: npm init -y Install the @clickhouse/client library: npm install @clickhouse/client To connect to ClickHouse, use the code below where we set the connection details and execute a simple SQL query that will return the first 10 records from the system table named system.tables: const { ClickHouse } = require('@clickhouse/client'); const client = new ClickHouse({ host: 'http://localhost:8123', username: 'default', password: 'PasswordforDefaultUser45435', database: 'default', }); async function connectAndQuery() { try { console.log('Successfully connected to ClickHouse Server!'); const rows = await client.query({ query: 'SELECT * FROM system.tables LIMIT 10', format: 'JSON', }).then((result) => result.json()); console.log('Query results:', rows); } catch (error) { console.error('Error Successfully connected to ClickHouse Server! or running the query:', error); } finally { console.log('Done.'); } } connectAndQuery(); Conclusion In today's article, we thoroughly explored how to connect to PostgreSQL, Redis, MongoDB, MySQL, and ClickHouse databases using Python, Go, and JavaScript. These languages can be used to create both web applications and microservices that utilize databases in their operation.
18 February 2025 · 23 min to read
JavaScript

How to Encode and Decode Strings Using Base64 in JavaScript

In modern web development, Base64 encoding plays a significant role in data exchange between clients and servers. This method allows converting binary data into string format, which is particularly useful for transmitting images, files, and other information in text or JSON format. In this guide, we will explore methods for encoding and decoding strings using Base64 in JavaScript, as well as share real-world examples and practical recommendations for applying this technique. What is Base64, and Why is It Useful? The Base64 encoding algorithm is a method of transforming binary data into an ASCII string format. This is achieved by dividing the original data into 6-bit blocks and replacing each block with a corresponding character from a predefined set. The main advantages of Base64 encoding include: Enabling the transmission of binary data over text-based protocols such as HTTP or JSON. Preventing issues related to incorrect handling of binary characters. Easy integration with various programming languages and systems. Base64 Encoding Algorithm The Base64 encoding method works as follows: The original binary data is divided into 3-byte (24-bit) blocks. Each 3-byte block is split into 4 blocks of 6 bits. Each 6-bit value is replaced by a corresponding character from the Base64 table. If the original data length is not a multiple of 3, = characters are added to make the resulting string length a multiple of 4. Encoding and Decoding Methods in JavaScript JavaScript provides built-in functions for working with Base64 and third-party libraries that offer extended functionality. Encoding a String to Base64 To encode a string to Base64 in JavaScript, the btoa() function is used. This function takes a string, encodes it in Base64, and returns the result: const originalString = "Hostman"; const encodedString = btoa(originalString); console.log(encodedString); // Outputs the encoded string This example demonstrates converting text to Base64 format. The built-in btoa() method is applied to the original value "Hostman", stored in the constant originalString. After processing, the result is stored in the encodedString variable and then displayed in the console as "SG9zdG1hbg==". This encoding method works efficiently with text containing basic ASCII characters but does not support Unicode. The transformation mechanism uses a special set of characters consisting of Latin alphabet letters, numbers, and two additional symbols: a plus (+) and a slash (/). Unicode String Encoding function encodeBase64Unicode(str) { return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) { return String.fromCharCode('0x' + p1); })); } const originalString = "Hostman 🚀"; const encodedString = encodeBase64Unicode(originalString); console.log(encodedString); As we can see in this example, the encodeBase64Unicode function encodes a string to Base64 with support for Unicode. First, encodeURIComponent is used, and then a regular expression converts the encoded characters using String.fromCharCode. Finally, btoa is applied. In this example, the string "Hostman 🚀" is encoded, and the result "SG9zdG1hbiDwn5qA" appears in the console. This method is necessary for correctly handling text containing Unicode characters, as the standard btoa() function cannot process them. Decoding a String from Base64 const encodedString = "SGVsbG8gV29ybGQ="; function decodeBase64Unicode(str) { return decodeURIComponent(Array.prototype.map.call(atob(str), function(c) { return '%' + c.charCodeAt(0).toString(16).padStart(2, '0'); }).join('')); } const decodedString = decodeBase64Unicode(encodedString); console.log(decodedString); As we can see, a variable encodedString is created containing a Base64-encoded string. The decodeBase64Unicode function decodes it by using Array.prototype.map and charCodeAt to convert it to Unicode. The result is stored in decodedString and displayed using console.log(). The console will display the message Hello World. Online Tools for Base64 Encoding and Decoding Many online tools allow you to quickly encode and decode strings and files without the need to write your own code. Some popular tools include: base64encode.org allows encoding and decoding of text and files. CyberChef is a multifunctional tool for data processing, including Base64. base64.guru provides detailed information and tools for working with Base64. Advantages of using online tools: Speed and convenience Ability to work without installing software Support for various data formats And some disadvantages: Data size limitations Possible data privacy concerns File Encoding and Decoding in Base64 Encoding a file in Base64 allows you to embed binary files, such as images or documents, directly into text formats like JSON or HTML. In JavaScript, this is done using the FileReader object. Example of encoding an image to Base64: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Base64 Image Encoding</title> </head> <body> <input type="file" id="fileInput" accept="image/*"> <img id="preview" src="" alt="Preview" /> <script> document.getElementById('fileInput').addEventListener('change', function(event) { const file = event.target.files[0]; if (file) { const reader = new FileReader(); reader.onload = function(e) { const base64String = e.target.result; console.log(base64String); document.getElementById('preview').src = base64String; }; reader.readAsDataURL(file); } }); </script> </body> </html> As shown in this example, we create a form with an <input> element for selecting images and an <img> element for previewing the image. JavaScript adds a change event listener to the input. When we select a file, a FileReader object is created to read the file and output the result as a Base64 string to the console, setting it as the source for the <img> element. In the screenshot, you can see an image with the Hostman logo, along with the Base64-encoded string displayed in the console after running the code. Comparison of Different Base64 Encoding Methods Encoding Method Description Advantages Disadvantages btoa() and atob() Built-in functions for encoding and decoding strings Easy to use Limited to ASCII characters FileReader Works with files for Base64 encoding and decoding Can handle files Asynchronous nature can complicate coding Third-party Libraries Libraries providing extended functionality Additional features and UTF-8 support Requires library inclusion Online Tools Web services for quick encoding and decoding Fast and convenient Limitations on data size and data privacy concerns Conclusion Base64 encoding and decoding strings using JavaScript is a popular method for converting data that is widely used in web development. Base64 allows you to convert binary data into a string format, which is easy to transmit through text-based protocols such as HTTP or WebSocket. The encoding algorithm splits data into 3-byte blocks and transforms them into four characters from a special alphabet. This ensures compatibility with systems that support only text content. Built-in JavaScript functions like btoa() and atob() simplify encoding and decoding, though you may need additional logic using TextEncoder and TextDecoder to handle Unicode.
12 February 2025 · 6 min to read
JavaScript

How to Use Functions in JavaScript

Functions are fundamental building blocks in JavaScript programming. They help users to encapsulate reusable chunks of logic for specific tasks. Functions also aid in organizing code effectively, which makes it modular as well as easier for maintaining applications. Functions decrease repetition, improve readability and promote better practices for debugging. Functions in JavaScript have different types that can depend on use cases as well as structure: Named Functions: Have explicit names, ideal for debugging. Anonymous Functions: Usually used as arguments for other functions. Arrow Functions: Streamlined syntax introduced in ES6 for cleaner and concise code. How Functions Work In JavaScript, functions are like objects of the Function type, which allows them to possess methods and properties. When invoked, a function makes a new execution context. The context comprises of: Variable Environment: Holds function arguments and local variables. Scope Chain: Ensures access to external variables within nested scopes. This Binding: An object which initiates the function call, influencing its behaviour in different contexts. Structure of a Function Functions in JavaScript typically comprise of a few components: Declaration Keyword: An arrow => or function is at the start of the syntax. Name: Functions may or may not have names based on their type. Parameters: Input variables passed into the function. Logic Body: The instructions are executed at the time the function runs. Example: function calculateSum(a, b) { return a + b; } Specialized Function Types Anonymous Functions Anonymous functions have no declared name and are usually utilized in temporary tasks, like call-back functions. Example: setTimeout(function() { console.log("Executing an anonymous function."); }, 1000); Arrow Functions Arrow functions possess simpler syntax, also in some cases help you avoid binding complexities. Example: const multiply = (x, y) => x * y; console.log(multiply(4, 5)); // Output: 20 Immediately Invoked Function Expressions (IIFE) These are executed just after being defined. IIFE are advantageous when initializing variables without polluting the global scope. Example: (function() { console.log("IIFE executed immediately!"); })(); Real-Life Applications Below are a few real-life applications. Handling Events Handling events are significant when it comes to managing events and making web pages interactive. Example: document.getElementById("button").addEventListener("click", function() { alert("Button clicked!"); }); Fetch Data through API Functions simplify the retrieval of data from external APIs and its subsequent processing. Example: fetch('https://api.example.com/data') .then(response => response.json()) .then(data => console.log(data)); Manipulating Data Structures Array methods like map, filter, and reduce use functions to enable efficient and concise data processing operations. Example: const numbers = [1, 2, 3, 4, 5]; const squares = numbers.map(num => num * num); console.log(squares); // Output: [1, 4, 9, 16, 25] Advanced Function Concepts Delving into Closures Closure is a type of function that can use variables from parent scope. This can happen even after a parent function has ended to run. Example: function counter() { let count = 0; return function() { count++; return count; }; } const increment = counter(); console.log(increment()); // Output: 1 console.log(increment()); // Output: 2 These are widely used in maintenance of private states and in functional programming paradigms. Recursive Functions Recursion enables a function to call itself for repetitive problem-solving. It’s particularly effective for operations like calculating factorials, generating Fibonacci numbers, or traversing data structures like trees. Example: function factorial(n) { if (n === 0) return 1; return n * factorial(n - 1); } console.log(factorial(5)); // Output: 120 Callback Functions Callbacks allow asynchronous execution, making them indispensable in event-driven programming. Example: function processUserInput(callback) { const name = prompt("Enter your name:"); callback(name); } processUserInput(name => alert(`Hello, ${name}!`)); Higher-Order Functions A key feature of JavaScript that significantly enhances its versatility is the use of higher-order functions. These functions either accept other functions as arguments, return a function, or both. They form the foundation of functional programming, enabling developers to write concise and expressive code. Example: const withLogging = (fn) => (...args) => { console.log(`Arguments: ${args}`); const result = fn(...args); console.log(`Result: ${result}`); return result; }; const add = (a, b) => a + b; const loggedAdd = withLogging(add); loggedAdd(3, 4); // Logs: Arguments: 3,4; Result: 7 This method is widely used in libraries like Lodash or RxJS for functional utilities and reactive programming. Memoization Memoization is an optimization technique that caches the results of function calls to avoid redundant computations, especially for functions with heavy processing or repeated calls that have the same input. Example: const memoize = (fn) => { const cache = new Map(); return (...args) => { const key = JSON.stringify(args); if (!cache.has(key)) { cache.set(key, fn(...args)); } return cache.get(key); }; }; const expensiveCalculation = (num) => num ** 10; const memoizedCalculation = memoize(expensiveCalculation); console.log(memoizedCalculation(2)); // Computed and cached console.log(memoizedCalculation(2)); // Retrieved from cache Performance Optimization with Function: Debouncing and Throttling Event-driven programming often requires optimization to prevent excessive function executions. Debouncing will make sure that a function will execute only after a delay, while throttling will make sure that a function will execute at regular intervals. Example: Debouncing const debounce = (fn, delay) => { let timer; return (...args) => { clearTimeout(timer); timer = setTimeout(() => fn(...args), delay); }; }; const handleResize = debounce(() => console.log("Resized!"), 500); window.addEventListener("resize", handleResize); Example: Throttling const throttle = (fn, limit) => { let inThrottle; return (...args) => { if (!inThrottle) { fn(...args); inThrottle = true; setTimeout(() => (inThrottle = false), limit); } }; }; const handleScroll = throttle(() => console.log("Scrolling!"), 200); window.addEventListener("scroll", handleScroll); Mastering Function Scope Functions operate within a specific scope that specifies accessibility of variable: Global Scope: Variables accessible throughout the program. Local Scope: Exclusive to a specific function. Hoisting in JavaScript JavaScript hoists function declarations, which allows them to be called before they are defined. Example: console.log(greet()); // Output: Hello! function greet() { return "Hello!"; } Techniques for Better Performance Limit Global Variables: Keep variable declarations as localized as possible. Decreasing Redundancy: Reuse logic effectively with modular functions. Debouncing and Throttling: Optimize event-driven functions to prevent excessive execution. Debugging Functions Efficient debugging includes: Using console.log() to trace execution flow. Leveraging browser developer tools for debugging. Writing unit tests to validate logic and identify issues. Conclusion Functions in JavaScript are vital for building scalable, maintainable applications. Mastering them requires understanding their various types, use cases, and techniques to optimize performance. Whether you have to handle events, fetch API data, or build complex algorithms, functions empower developers to write efficient as well as reusable code. In addition, on our cloud application platform you can find frontend apps, such as React, Angular, Vue and more.
21 January 2025 · 7 min to read

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start.
Email us
Hostman's Support