Navigate back to the homepage

how to add string to array in node js?

Infinitbility
Node
July 8th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To add string to array in node js, just use the push() method it will add your data into an array as the last element.

Like the following example, we can add string to array using the push() method.

1let arr = ["infinitbility","aguidehub"];
2
3arr.push("sortoutcode");
4
5console.log(arr); // ['infinitbility', 'aguidehub', 'sortoutcode']

Today, I’m going to show you How do i add string to array in node js, as above mentioned here, I’m going to use the push() method for adding new elements into the array.

Let’s start today’s tutorial how do you add string to array in node js?

In this example, we will do

  1. Create a sample array variable with sample data
  2. Add a new element into the array variable using the push() method
  3. ouput return in the response

Let’s write code…

1var http = require("http");
2
3//create a server object:
4http
5 .createServer(function (req, res) {
6 let newArr = ["infinitbility","aguidehub"];
7 newArr.push("sortoutcode");
8
9 res.write(JSON.stringify(newArr));
10 res.end(); //end the response
11 })
12 .listen(8080); //the server object listens on port 8080

Note: We used JSON.stringify() method only to show output in the response

As mentioned above, we are taken the example of an array of strings, use the push() to add an element, and printed it on the screen.

I hope it helps you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to kill process using port in windows?

An example of kill process using port in windows with a step-by-step example.

July 8th, 2022 · 1 min read

how to check empty string in node js?

An example of check empty string in node js with a step-by-step example.

July 7th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]