Introduction JavaScript console.log() Function with Examples

The console.log() is a function in JavaScript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user.

Syntax


console.log(X);

Parameter(s)

  • It accepts a parameter which can be an array, an object or any message.

Return value

  • It returns the value of the parameter given.

JavaScript console.log() with Example

Example 1

Print A Variable To The JavaScript Console


console.log("Selam","Poftut.com")
//Output will be Selam Poftut.com
 
sitename="poftut.com";
 
console.log(sitename);
//Output will be poftut.com
 
username="ismail";
 
console.log(username);
//Output will be ismail
 
age=35;
 
exist=false;
 
console.log(age);
//Output will be 35
 
console.log(exist);
//Output will be false

Output

This is image title

Example 2

Print String To The JavaScript Console


console.log("poftut.com");
//Otput will be poftut.com
 
console.log("poftut.com","ismail baydan");
//Otput will be poftut.com ismail baydan
 
console.log("poftut.com","ismail baydan","ahmet ali");
//Otput will be poftut.com ismail baydan ahmet ali
 
console.log("poftut.com","ismail baydan","ahmet ali","Elif ecrin");
//Otput will be poftut.com ismail baydan ahmet ali Elif ecrin
 
console.log("poftut.com","ismail baydan","ahmet ali",9);
//Otput will be poftut.com ismail baydan ahmet ali 9

Output

This is image title

Example 3

Print Number/Integer To The JavaScript Console


console.log()
//Output will be 0
 
console.log(10)
//Output will be 10
 
console.log(101)
//Output will be 101
 
console.log(101.5)
//Output will be 101.5
 
console.log(-101.5)
//Output will be -101.5
 
console.log(-101.5987654321)
//Output will be -101.5987654321
 
console.log(-101.5987654321123456789)
//Output will be -101.59876543211234


Output

This is image title

Example 4

Print Char To The JavaScript Console
String variables consist of characters where it is depicted as character data type. We can also print the chars to the browser console like below.


console.log('i')
//Output will be i
 
console.log('i','s');
//Output will be  i s
 
console.log('i','s','m');
//Output will be  i s m
 
console.log('i','s','m','a');
//Output will be i s m a
 
console.log('i','s','m','a','i');
//Output will be  i s m a i
 
console.log('i','s','m','a','i','l');
//Output will be i s m a i l

Output

This is image title

Example 5

Print Array To The JavaScript Console

Arrays are used to store multiple values like a list. We can also print the array contents to the browser console with the console.log() function. The array elements can be same type of different types.


names=["ismail"];
console.log(names);
//Output will be ["ismail"]
 
names=["ismail","elif"];
console.log(names);
//Output will be ["ismail", "elif"]
 
names=["ismail","elif","ahmet"];
console.log(names);
//Output will be ["ismail", "elif", "ahmet"]
 
numbers=[1,2,3,4];
console.log(numbers);
//Output will be [1, 2, 3, 4]
 
myarr=["ismail","ahmet",1,2,3];
console.log(myarr);
//Output will be ["ismail", "ahmet", 1, 2, 3]

Output

This is image title

Thanks for reading !

#js #javascript

Introduction JavaScript console.log() Function with Examples
7.95 GEEK