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?

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.

53 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

47 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.

63 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!

30 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

1 Like

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

3 Likes

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!

9 Likes

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.

Anything inside quote is a string which will stay same while the numbers will be added like in your question if its inside string print ‘3 + 4’
instead if its not inside quote it would print 7

‘3+4’ //prints ‘34’(because in quotes it consider it as string
3+4 //prints 3+4

console.log(3+4) << will return the value of adding 3 to 4 which is 7

console.log(‘3+4’) << this one will return the string 3+4 because everything inside the quotation marks is string

The first is a numerical operation & displays the result of the equation. The second in quotations marks is treated as a string and displayed as is.

1 Like

String = items that print as-is (not calculations), and uses ’ ', for example: (‘8’), (‘dog’), or (‘2 + 40’)

A calculation is not a string and does not use ’ ', for example, (2 + 4), (5 * 10), or (9 / 3)

Good question!

see if u write the code- console.log(3+4); it will give u the answer as 7.and if u write console.log(‘3+4’); it’ll print 3+4.the difference between em is that one is wrapped with quotes which we usually call it as strings.and the other has the code without quotes so its a number.

console.log(3+4); // It prints out the sum of two numbers, 7.
console.log(‘3+4’); It prints out 3+4 because ‘3+4’ is string of text in quotation.

1 Like