×

JavaScript Console log Multiple variables

What does console.log() do in JavaScript?

The console.log() is a function in JavaScript language. It is used to:

  • Print any message in the console which is visible to the user
  • Print the values of variables defined

Advantages of console.log()

  • Multiple browsers support this function, for example, Google Chrome, Safari, Microsoft Edge/Internet Explorer, Firefox, Opera etc.
  • This function is primarily used for testing purposes.

NOTE: When F12 key is pressed on the website, the console view is visible.

Syntax:

console.log(“message”)

Here, “message” is any string.

Or

console.log(var)

Here, “var” denotes any variable which has a value stored in it.

The function can take any array, object or value as a Parameter. It then returns the value according to the parameter.

Example:

<script>
var name = "Javatpoint";
console.log(name);
</script>

Output:

Javatpoint

Example:

<script>
console.log(“Hello”);
</script>

Output:

Hello

Multiple variables in console.log()

We know, that when we pass a single variable to the console.log(), the following output is generated:

Example:

<script>
    let x = 2;
    console.log(x);
</script>

Output:

2

But suppose, if we have multiple variables, instead of writing the function of console.log() multiple times, we can include all the variables in a single line.

Example:

<script>
    let x = 2;
    let y = 3;
    let z = 6;
    console.log(x,y,z);
</script>

Output:

2 3 6

Tips to use while using multiple variables in console.log()

1. Referring the multiple variables by name

When multiple variables are passed in console.log(), it sometimes gets difficult to recognize which variable corresponds to a log in the console.

Example:

<script>
    		let x = 2;
    		let y = 3;
    		let z = 6;
   	 	console.log(x,y,z);
</script>

Output:

2 3 6

Here, it is difficult to identify what values corresponds to which variable.

To overcome that, use curly braces { } along with the variable to make an association between the variable and value.

Example:

<script>
    		let x = 2;
    		let y = 3;
    		let z = 6;
    		console.log( {x, y, z} );
</script>

Output:

{x: 2, y: 3, z: 6}

Similarly, curly braces can be used with the objects as well.

Example:

<script>
   		let obj1 = {
  		name: 'Javatpoint',
 		 age: 18
                       }
                       console.log(obj1)
                       console.log({obj1})
</script>

Output:

{name: "Javatpoint", age: 18}	
{obj1: {…}}

2. Formatting

When we want to print a message containing multiple variables along with a message, we can format console.log() accordingly by:

  • Using specifiers like %s, %i, %f etc.

Example:

<script>
   let user = 'Rahul';
   let age = 25;
   console.log('%s is %i years old', user, age);
  </script>

Output:

Rahul is 25 years old

Here, we are formatting a message containing a string and an integer, therefore %s is used for “user” and %i is used for “age”.

Following is a list of other specifiers, that can be used as well:

SpecifierDescription
%sUsed for string elements
%d or %iUsed for integer elements
%fUsed for float type of elements
%oUsed for optimally useful formatting in elements
%OUsed for generic JavaScript object formatting in elements
%cUsed to add additional CSS formatting
  • Concatenating the variables

When we want to print two or more strings together,”+” operator can be used as well to combine them together.

Example:

<script>
let x = 'Java';
let y = 't';
let z = 'point';
let year = 2021;
console.log(x+y+z);
console.log(x+y+z+year);
</script>

Output:

Javatpoint
Javatpoint2021

NOTE: If all the variables contain integers, then “+” operator won’t concatenate the variables, it will simply add the integers and display the sum.

3. Adding CSS styles

By using the %c specifier in the console.log(), you add corresponding CSS styles along with specifier. Various browsers support the CSS formatting.

Example:

If we want to change the font size, font style and underline the text.

<script>
console.log('%cFormatted message', 'font-size: 25px; font-style: italic; text-decoration: underline;');
</script>

Output:

JavaScript Console Log Multiple Variables

Here, The specifier %c applies the CSS styles , 'font-size: 25px; font-style: italic; text-decoration: underline;'.

4. Adding interactive logs along with the messages

The %o specifier (Used for optimally useful formatting in elements) can insert objects, DOM elements, and normal text into a textual message.

Example:

<script>
const obj = {
name: 'Rahul',
	surname: 'Gupta'
};
console.log('Hello, new user %o', obj);
</script>

Output:

Hello, new user {name: "Rahul", surname: "Gupta"}

Related Topics

Javascript Page Redirect

The window.location object can be used to get the current page address (URL) and to redirect browser to new page. In some websites when we visit, we face a situation where we...

3 minutes read.

JavaScript dropdown onchange

What is onchange event? It is an event in JavaScript used to make the web pages dynamic. The onchange event gets triggered whenever the value of an event changes. It usually...

3 minutes read.

Javascript Print

JavaScript help's the functionality using the print function of window object. In JavaScript print function window.print() print the current web page when it executed. We can call function directly using onclick...

1 minute read.

JavaScript Arrays

JavaScript Arrays: JavaScript Arrays are a type of variables that allows us to store the individual or the group of values under a single variable. What is an Array in JavaScript? The array...

6 minutes read.

Javascript comment

Introduction In programming languages, comments are a method to include meaningful annotations within the code that the compiler or interpreter will ignore at run time. That is to say, though comments...

7 minutes read.

JavaScript Image resize before upload

Image resizing can be a quite tedious task, so it is usually done on the server-side. The resized image file is then delivered to the client side. However, sometimes the...

3 minutes read.

Javascript Tutorial

JavaScript is a light-weight, dynamic scripting language that is mainly employed to create interactive front-end web applications. It was originally developed in 1995 by Brendan Eich at Netscape Communications Corporation...

5 minutes read.

Javascript loops

Loops in JavaScript: Loops are used to execute a specific piece or block of code repeatedly until the given condition is satisfied. What is a Loop? Almost all loops contain a certain...

7 minutes read.

JavaScript Console log Multiple variables

What does console.log() do in JavaScript? The console.log() is a function in JavaScript language. It is used to: Print any message in the console which is visible to the userPrint the values...

3 minutes read.

Javascript Password Validation

Here we see that how to validate any password field. Like password field can be blank and length of password is minimum 8 characters. Example <!DOCTYPE html>   <html>   <head>   <script>   functionpass_validation()   {     var password=document.myform.password.value;     if (password==null || password=="")   {     alert("password can't be blank");     return false;     }   else if(password.length<8)   {     alert("Password must be at least 8 characters long.");     return false;       }     }     </script>   </head>   <body>   <form name="myform" method="post" action="register.php" onsubmit="return pass_validation()" >   Password: <input type="password" name="password">   <input type="submit" value="submit">   </form>   </body>   </html> Try Now ← Prev Next → ...

1 minute read.

Javascript Event

Events are things that happen, usually user action, that are associated with an object. All object have properties and methods. Some objects also have events. The event handler is a command that is...

1 minute read.

Javascript Operators

The concept of operators is the basis of nearly all programming languages used in today's computing systems. Operators facilitate the execution of certain functions. For instance, the (+) symbol is...

8 minutes read.

JavaScript Function

An important part of JavaScript is the ability to create new functions within <script>.....</script> tag. To declare a function in JavaScript using function keyword. We call JavaScript function multiple times to reuse the...

3 minutes read.

What is Ternary Operator in JavaScript?

In some cases, a ternary operator can be used instead of an if-else expression. A ternary operator examines a condition and then runs a block of code in response to...

4 minutes read.

Javascript If Else

Introduction The if-else statement is an important control structure for decision-making in code based on some conditions. It gives programmers the ability to add decision-making functionality into their programs. Using if-else,...

6 minutes read.

Javascript Object

An object is an entity having state and behavior. JavaScript is an object oriented scripting language. JavaScript is template based not class based but we can create object directly. Syntax to...

2 minutes read.

Javascript Data Types

An Overview of JavaScript Data Types In computing, there are a number of data types, including numbers, strings, and booleans, to name a few. The role of these data types in...

5 minutes read.

Javascript Example

What is JavaScript? JavaScript is a highly versatile programming language. It provides the capability to add interactivity to static web pages, which are designed with HTML and CSS. It also provides...

5 minutes read.

JavaScript Time Picker Demo

Time Pickers in JavaScript are lightweight and mobile-friendly controls that let the users enter or select date and time values. These values can be from a pop-up calendar or a...

5 minutes read.

Javascript Window Object

The window object represents a window in browser. An object of window is created automatically by the browser. Window is the object of browser, it is not the object of JavaScript. The...

3 minutes read.