How to print two variables in same line in javascript

How to print string and integer value in same line. I can print in separate line

daljeet

16

I want to print like this daljeet 16

public class hello {

    public static void main(String[] args) {

        String name = "daljeet";

        int age = 16;


        System.out.println(" " +name +age);



    }

    }

Program output daljeet16

How to get space between two variables ?

fabian

75.4k12 gold badges79 silver badges110 bronze badges

asked Aug 3, 2018 at 4:41

2

public class hello {

 public static void main(String[] args) {

  String name = "daljeet";

  int age = 16;

  System.out.println(name + " " + age);
 }
}

answered Aug 3, 2018 at 4:43

jh314jh314

26.2k15 gold badges61 silver badges81 bronze badges

The lazy way would be to just put a space at the back of the variable name i.e "daljeet ". Otherwise, you could write the following:

System.out.println(name + " " + age);

Logan

9261 gold badge12 silver badges18 bronze badges

answered Aug 3, 2018 at 4:45

How to print two variables in same line in javascript

  1. HowTo
  2. JavaScript Howtos
  3. Declare Multiple Variables in a Single Line in JavaScript

Created: March-25, 2022

  1. Variables in JavaScript
  2. Declare Single Variable using JavaScript
  3. Declare Multiple Variables in JavaScript
  4. Declare Multiple Variables in a Single Line in JavaScript
  5. Declare Multiple Variables in a Single Line Using De-structuring Algorithm in JavaScript

This article will describe to its readers how they can declare or define multiple variables in JavaScript. To begin with, let’s have a look at what a variable is and how to declare it in JavaScript with different conditions and scenarios.

Variables in JavaScript

A variable is a memory area in the program where values may be stored and retrieved as needed. Each variable has its name.

The data in the variables can be changed at any time during the program’s execution, so JavaScript variables are not flexible. A variable can be declared using let, var, and const.

Declare Single Variable using JavaScript

Example:

let age = 5;

It only gives information about a single variable.

What if more storage units or variables are needed in our code? How to assign multiple variables in code?

Declare Multiple Variables in JavaScript

The answer to the above questions is given here. You can declare multiple variables in JavaScript.

There are different approaches for declaring multiple variables in your JavaScript program, and they are described below. The most common technique of declaring and initializing JavaScript variables is to write each variable on its line, as in:




JavaScript Variables

In this example, age, income, and sum are variables.

Click on the following link to see the working of the code segment.

Declare Multiple Variables in a Single Line in JavaScript

You can declare multiple variables in a single line. However, there are more efficient ways to define numerous variables in JavaScript.

First, define the variables’ names and values separated by commas using just one variable keyword from var, let or const.

Example:




Click on the following link to see the working of the code segment.

Although the declaration above only uses one line, it is more difficult to comprehend than separate declarations. The above method can be generalized to:

var [name,age,income] = ["XYZ",18,500]

Declare Multiple Variables in a Single Line Using De-structuring Algorithm in JavaScript

With the introduction of ES2015 (also known as ES6), the de-structuring algorithm was added to JavaScript, and it has quickly become one of the most valuable aspects of the language for two reasons:

  • It aids in the avoidance of code repetition.
  • It keeps your code tidy and straightforward to comprehend.

The de-structuring algorithm is a JavaScript method. This lets you extract values from arrays or attributes from objects and save them in other variables.

Although the declaration above only uses one line, it is more difficult to comprehend than separate declarations. The above method can be generalized to:

var [name,age,income] = ["XYZ",18,500]

These variables can then be printed using the console.log() command giving access to the user about information or data used or stored in variables accordingly.

console.log(name); // "XYZ"
console.log(age); // 18
console.log(income); // 500

Click on the following link to see the working of the code segment.

Output:

How to print two variables in same line in javascript

How do I print two variables on the same line?

You can use the cat() function to easily print multiple variables on the same line in R. This function uses the following basic syntax: cat(variable1, variable2, variable3, ...) The following examples show how to use this syntax in different scenarios.

Can you define multiple variables in one line JavaScript?

To define multiple variables on a single line with JavaScript, we can use the array destructuring syntax. const [a, b, c, d] = [0, 1, 2, 3]; to assign a to 0, b to 1 , c to 2, and d` to 3 with the array destructuring syntax.

How do you print a line in JavaScript?

printing in a single line in javascript.
var s = "";.
for(var i = 1; i < 11; i += 1) {.
s += i + " ";.
console. log(s);.

Can you assign multiple variables in JavaScript?

You can set multiple variables to the same value in JavaScript by using the equal sign (=) consecutively between the variable names and assigning a single value at the end when declaring the variables. var c = 10; var b = c; var a = b; You can also declare the variables first and assign the value later.