Return Something to Me! in JavaScript

  • Share this:

Return something in JavaScript, you can use the `return` statement. The `return` statement terminates the function and returns a value to the calling code.

Here is an example of how to use the `return` statement to return a string:

function greet() {
  return "Hello!";

var greeting = greet();
console.log(greeting); // Output: "Hello!"

You can also use the `return` statement to return the result of a calculation:

function add(a, b) {
  return a + b;

var result = add(2, 3);
console.log(result); // Output: 5

You can return any type of value from a function, including strings, numbers, booleans, arrays, and objects:

function getArray() {
  return [1, 2, 3];

var arr = getArray();
console.log(arr); // Output: [1, 2, 3]

function getObject() {
  return {
    name: "John",
    age: 30

var obj = getObject();
console.log(obj); // Output: { name: "John", age: 30 }



About author
I am a professional web developer. I love programming and coding, and reading books. I am the founder and CEO of StorialTech.