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
Created: March-25, 2022 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. 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 Example: 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? The answer to the above questions is given here. You can declare multiple variables in JavaScript.Variables in JavaScript
let,
var,
and const.
Declare Single Variable using JavaScript
let age = 5;
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.
var age = 5;
var income = 6;
var sum = age + income;
document.getElementById["data"].innerHTML =
"The value of sum is: " + sum;
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:
var name = "XYZ", income = 500, age = 23;
document.getElementById["data"].innerHTML = name + " " + income + + age;
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: