Skip to main content

Command Palette

Search for a command to run...

Debugging Part1:Using the JavaScript Console to debug. Using typeof to Check the Type of a Variable

Updated
2 min read
Debugging Part1:Using the JavaScript Console to debug.
Using typeof to Check the Type of a Variable
S

I am self-taught Full stack Developer learning through online resources. I would love to connect with people and have a chat. The majority of my learning comes from online resources. I started this blog so that I can document my journey as a self-taught developer, its hardships, tips, and tricks.

Hello everyone hope you are all doing well, My name is Surya L.

I have divided the debugging topics in many parts to teach you Debugging in detail.

In this blog we will use JavaScript Console to debug and typeof to Check the Type of a Variable in order to debug the code.

Using the JavaScript Console to debug.

  • Debugging with the console.log() method will likely be the most helpful tool since it prints out the contents of the parentheses.
  • You can display intermediate values of variables by placing it at strategic points in your code.

  • The console.log can be used to verify that variables and functions work as expected according to the code.

Example for debugging with console.log

function multiThree(num) {
  return num * 3;
}
console.log(multiThree(5));
//The Output would be 15
  • As you can see, with console.log, you can see the output of the function.
  • Also check if it works.

Using typeof to Check the Type of a Variable

  • You can use typeof to determine a variable's data structure or type.
  • When dealing with multiple data types, this is useful for debugging.

  • For Example:

let num1 = 9; 
//data type : Number

let num2 = "3"; 
//data type : String

let summation = num1 + num2; 
//expected output: 9+3=12

console.log(summation);

console.log(typeof num1); //output: number

console.log(typeof num2); //output string

console.log(typeof summation);

Explanation:

  • console.log(summation) with an expected output: 9+3=12
  • But we get output: 93 because we are adding a number and a string which is not possible.
  • But we expected output: 12 because we should number and a number which should be possible.

  • console.log(typeof summation); output: string , but we expected output: number.

  • As you saw with typeof, we can see that we can't add a number and a string and we can fix the code.

Thanks for reading the blog. Do let me know what you think about it.

You can connect me with Showwcase Twitter Blog GitHub Contact Me

JavaScript

Part 13 of 50

This blog is all about JavaScript topics from Basics to Advance, along with interview question.

Up next

Learn the concept of Hoisting for Variable and Functions in JavaScript.

With Simple Examples for each cases.

More from this blog

S

Surya's Web Blogs

68 posts

Hello Everyone ,I am SURYA. This blog Will be all About Full stack dev things along with tips and tricks. And a glimpse of the twists of my Personal Life