• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using JavaScript to Return String

Using JavaScript to Return String

September 29, 2022 Leave a Comment

In JavaScript, to return a string in a function, we simply need to use the return statement. Here is a very simple example.

function someFunction(){
  return "someString";
};

If we were to call the function, someFunction(), then the string "someString" would be returned.

function someFunction(){
  return "someString";
};

console.log(someFunction());

#Output
someString

The return statement will end the function right when it is called and return whatever value is next to it.

And that’s how easy it is to return a string. Let’s see a couple more examples.

function examplefunction1(name){
  var nameString = "Hello " + name;
  return nameString;
};

function examplefunction2(){
  return "Function ends here";
  var anotherString = "This code and the return statement below it will not run";
  return anotherString;
};

function examplefunction3(){
  var currDate = new Date();
  var month = currDate.toLocaleDateString('en-US', { month: 'long'});
  return month;
};

console.log(examplefunction1("Henry"));
console.log(examplefunction2());
console.log(examplefunction3());

#Output
Hello Henry
Function ends here
September

Return Multiple Strings in JavaScript

If we wanted to return more than one string using JavaScript, we could simply put the strings in an array and return the array. Here is a simple function to do this.

function someFunction(){
  var string1 = "This is a string.";
  var string2 = "This is another string.";
  var arr = [];
  arr.push(string1);
  arr.push(string2);
  return arr;
};

console.log(someFunction());

#Output
["This is a string.", "This is another string."]

Hopefully this article has helped you understand how to use JavaScript to return a string.

Other Articles You'll Also Like:

  • 1.  Remove the First Element From Array in JavaScript
  • 2.  JavaScript Square Root with Math.sqrt() Method
  • 3.  Set Checkbox as Checked in JavaScript
  • 4.  How to Use JavaScript to Change Button Text
  • 5.  Remove Empty Strings from an Array in JavaScript
  • 6.  Convert an Array of Values into a String Without Commas in JavaScript
  • 7.  Using JavaScript to Convert Double to Integer
  • 8.  JavaScript Random Number – How to Generate a Random Number In JavaScript
  • 9.  Using JavaScript to Detect Window Resize
  • 10.  Using JavaScript to Remove Apostrophe From a String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy