What is the difference between printing `3 + 4` and `'3 + 4'`?

example. console.log(3 + 4); // Prints 7

“if we want to print characters 3 + 4, wrap in quotes and print as a string” - This is what the lesson states.

I thought we did print characters 3 + 4 above, but they are not in strings. What is the difference?

1 Like

console.log(3 + 4); will print 7 onto the console. console.log(‘3 + 4’); will print 3 + 4 onto the console. i dont entirely understand the question but i hope this clears up your confusion.

29 Likes

I also don’t fully understand your question, but from what I do understand, I think you are asking:

Why is it when we type console.log(7); it prints 7, but then when we type console.log(3 + 4); the answer of 7 is printed instead of 3 + 4 like in the case of just 7.

From what I am understanding of the lessons so far, arithmetic operators, like + automatically perform tasks in the code when the computer reads it. So the computer sees that there is a + and it is built into the language that when it comes across a + it needs to add the numbers on either side of it. When you wrap the content inside of the parentheses ( ) with quotations ’ ', then the computer sees that as a string, and so it knows to print out exactly what is typed in the string, and not to do the calculation.

I hope this helps, I have just started with JS, so the above is my understanding at the moment.
Kevin

18 Likes

I see the confusion I think.

console.log(3 +4); That will take 3 and 4 and add them together.

console.log(‘3 + 4’); The quotation marks identify everything in between it as a string and not an addition problem, it no longer sees + sign as an arithmetic operator but just another part of a string to be printed out.

console.log(‘3’ + ‘4’); The output is 34 because it sees 3 and 4 as two strings to combine not add the numbers. You could change that to console.log(‘h’ + ‘i’); and the result would be hi.

Remember a string is just any text inside single or double-quotes.

I hope that adds to the discussion.

32 Likes

the difference is that the quotes is are not numbers but are characters and the one with quotes are numbers.

The difference is that both will print but in the first console.log(3+4), it will print it out as 7
but in the second console.log(‘3 + 7’) it has become a string because of the quotes surrounding it. I don’t really understand the question you asked but i hope this helped

1 Like

I just started learning JS

console.log(3+4),
console.log('3+4');
console.log('3'+'4');
  1. The first line of code is an arithmetic operator telling the computer to print the result of 3+4.
    The result will be 7

  2. The Second is a string, telling the computer to print what is inside the parenthesis (as text).
    The result will be 3+4

  3. The last is a combination of two strings, Telling the computer to display the string next to each other (combine) the texts.
    The result will be 34

that is my own understanding.
Happy coding!

4 Likes

the difference is if you wrap the strings in qoutes then they get printed as they are without being added indifference from unwrapen in qoutes

How do you combine strings and arithmetic operators? I.e. Like I want my print to say 3+4 and then also do the calculation and post the result next to the string. so in essence effectively written like 3+4=7

Hello;

This is day one of me learning JS, you asked a very good question that I myself didn’t think to ask on module 5 of JS. In order to concatenate (link) a string in console.log(), all you have to do is include a comma in between the string ‘3 + 4 =’ AND 3+4 operator.

Such as:
console.log(‘3 + 4 =’, 3+4); // Concatenates a String and Operator

It took a few minutes of playing around with the code and rummaging through the web, but I found at least this one solution, perhaps there are better practices.

Thanks for the question!

1 Like

To Answer this Question, when you do console.log(3+4) it returns an interger (number) value of 7, but when you do console.log(‘3+4’) it will return the string(word) value being 3+4. The ‘’ represents a string

3+4 means arithmetic addition of 3 and 4 which gives 7.
‘3+4’ means creating a string of “3+4”

when you type console.log(3 + 4) the data that you have input is classified as a numeric data type hence the two numbers will be treated as integers and addition will take place and the answer 7 will be printed or logged onto the console.
But as we the previous lesson taught us console.log(‘3 + 4’) or console.log(“3 + 4”) both will be treated as string data type as they are encased in single(double) quotes. Hence the computer will print 3 + 4.