0
0
Javascriptprogramming~5 mins

String concatenation in output in Javascript

Choose your learning style9 modes available
Introduction

String concatenation helps you join words or messages together to show in the output.

When you want to greet someone by name, like 'Hello, John!'
When you want to show a sentence made from different parts, like 'I have 3 apples.'
When you want to combine a number and text to explain a result.
When you want to build a message step-by-step before showing it.
Syntax
Javascript
let message = 'Hello, ' + name + '!';
console.log(message);
Use the + sign to join strings and variables together.
Make sure to add spaces inside quotes if you want spaces in the output.
Examples
Joins two strings directly to print 'Hello, world!'.
Javascript
console.log('Hello, ' + 'world!');
Joins a string and a variable to greet Alice.
Javascript
let name = 'Alice';
console.log('Hi, ' + name + '!');
Joins text and a number to show a message count.
Javascript
let count = 5;
console.log('You have ' + count + ' new messages.');
Sample Program

This program joins strings and variables to create a greeting and an info message, then prints both.

Javascript
let firstName = 'John';
let lastName = 'Doe';
let age = 30;

let greeting = 'Hello, ' + firstName + ' ' + lastName + '!';
let info = 'You are ' + age + ' years old.';

console.log(greeting);
console.log(info);
OutputSuccess
Important Notes

Remember to convert numbers to strings automatically by using + with strings.

You can also use template literals (backticks) for easier string joining, but + is simple and clear.

Summary

Use + to join strings and variables for output.

Add spaces inside quotes to keep words separate.

String concatenation helps build clear messages for users.