10 The this Keyword 1 - please explain backticks


#1

as mentioned here, there is no explanation or hint to use backticks (`) instead of single quotes (’)

I still do not understand the difference, but the computer sure does. I wish codecademy had explained them before using them. I can hardly tell ` and ’ apart!


#2

This has long been explained:

https://www.codecademy.com/courses/learn-javascript-introduction/lessons/variables/exercises/string-interpolation-ii?action=resume_content_item&course_redirect=introduction-to-javascript

and coming back throughout the course, so at certain point the explanation stops.


#3

Please elaborate where it is explained. I do not see it. It says what to do but not why


#4

In the newest version of JavaScript (ES6) we can insert variables into strings with ease, by doing two things:

  1. Instead of using quotes around the string, use backticks (this key is usually located on the top of your keyboard, left of the 1 key).
  2. Wrap your variable with ${myVariable}, followed by a sentence. No +s necessary.

ES6 string interpolation is easier than the method you used last exercise.

Using string interpolation allows us to insert variable into strings more easily, otherwise you get this mess:

var x = "world"
console.log("hello " + x + "!")

in which you can easily miss a " or + symbol


#5

Making something easier doesn’t quite explain it for me.

how is this…

let person = {
  name: 'Tyron'
  sayHello: function() {
    return `Hello, my name is ${this.name}`;
  },
};

different from this…

let person = {
  name: 'Tyron'
  sayHello: function() {
    return 'Hello, my name is ${this.name}';
  },
};

I am trying to understand from the computer’s perspective why they operate differently


#6

you use different symbols for enclosing the string. the backticks let javascript know you want to use string interpolation, enclosing in apostrophes will just print the string as it is.


#7

So a backtick tells Javascript to look for the interpolation? I guess that was obvious but explaining it as a direct causal link between actions makes it easier for me to understand. Thank you very much for your help.


#8

yes. But in the lesson i linked to as well?


#9

What about the lesson? The linked lesson explains that backticks allow us to insert variables into strings but this is not a causal relationship. The syntax matters here - allowing something vs causing something.


#10

syntax always matters.

But the lessons state that if you want to use es6 string interpolation, you must enclose the string in backticks. There is a reason this is stated in the lesson.


#11

I understand that, but explaining what something is does not explain why or how it works. Allowing something to happen is essentially keeping the door open. The question is, who opened the door? Backticks don’t allow the use of string interpolation, anymore than the system allows our code to run. I now know that backticks directly communicate to the computer that you want to use string interpolation. This causal relationship helps me better understand what is going on. Again, thank you for your help.


#12

that is a very abstract way of putting it

JS is an interpreted language, meaning someone wrote a set of instructions which interpreter the code into something the computer can understand. Given there was a dire need for better concatenating strings and variables, the interpreter was updated in 2015 (es6), now when the interpreter sees the backticks in your code, it know it first need to extract the variable and parse them in the string

Computers contain a lot of abstraction, its impossible to understand it all in detail.


#13

:ok_hand:

Very interesting background info. I know its impossible to understand it all, but every little bit helps. Hope you have a great day!