Skip to content

JavaScript function return string

  • by

In JavaScript, functions can return string values. A function can take parameters and manipulate them to return a string using string concatenation or string interpolation. This string can then be used in other parts of the program.

function myFunction(parameter1, parameter2) {
  // Function body
  let myString = parameter1 + " " + parameter2;
  return myString;
}

In JavaScript, to define a function that returns a string, you can use the function keyword followed by the name of the function, a set of parentheses for any parameters, and the function body enclosed in curly braces. Within the function body, you can use the return keyword to specify the string that the function should return.

function getGreeting(name) {
  return "Hello, " + name + "! How are you today?";
}

let greeting = getGreeting("Alice");
console.log(greeting); 
// Output: "Hello, Alice! How are you today?"

JavaScript function return string example

Simple example code.

function getGreeting(name) {
  if (name.length > 10) {
    return "Hello, " + name + "! That's a long name.";
  } else {
    return "Hello, " + name + "! How are you today?";
  }
}

let greeting1 = getGreeting("Alice");
console.log(greeting1);

let greeting2 = getGreeting("Bob Smith");
console.log(greeting2);

Output:

JavaScript function return string

Another example

Let’s say you’re building a weather app that displays the current weather conditions for a user’s location. You might have a function that retrieves the current weather data from a weather API and returns a string describing the current weather conditions.

<!DOCTYPE html>
<html>
<head>
	<title>Weather App</title>
</head>
<body>
	<h1>Weather App</h1>
	<form>
		<label for="city">Enter a city:</label>
		<input type="text" id="city" name="city">
		<button type="button" onclick="getWeather()">Get Weather</button>
	</form>
	<div id="weather-dashboard"></div>

	<script>
		// Function to retrieve the current weather data for a specified city from a weather API
		function getCurrentWeather(city) {
		  // Make a request to the weather API to retrieve the current weather data for the specified city
		  let weatherData = makeAPIRequest(city);

		  // Extract the relevant weather data from the API response
		  let temperature = weatherData.current.temperature;
		  let conditions = weatherData.current.conditions;

		  // Create a string describing the current weather conditions
		  let weatherString = `The current temperature in ${city} is ${temperature} degrees and the conditions are ${conditions}.`;

		  // Return the weather string
		  return weatherString;
		}

		// Function to handle the button click event and display the weather conditions for the specified city
		function getWeather() {
		  // Get the value of the city input field
		  let city = document.getElementById('city').value;

		  // Call the getCurrentWeather function with the specified city
		  let currentWeather = getCurrentWeather(city);

		  // Display the weather conditions on the weather dashboard
		  let weatherDashboard = document.getElementById('weather-dashboard');
		  weatherDashboard.innerHTML = `<p>${currentWeather}</p>`;
		}
	</script>
</body>
</html>

Note: in this example, the makeAPIRequest function and getUserLocation function are not defined. These would be additional functions that you would need to define in your web application to make requests to the weather API and retrieve the user’s location, respectively.

Comment if you have any doubts or suggestions on this JS function topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading