ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • String Interpolation
    Study/JavaScript 2020. 4. 29. 10:26

    String Interpolation

    In the ES6 version of JavaScript, we can insert, or interpolate, variables into strings using template literals. Check out the following example where a template literal is used to log strings together:

     

    const myPet = 'armadillo'; 
    console.log(`I own a pet ${myPet}.`); 
    // Output: I own a pet armadillo.

     

    Notice that:

    • a template literal is wrapped by backticks ` (this key is usually located on the top of your keyboard, left of the 1 key).
    • Inside the template literal, you’ll see a placeholder, ${myPet}. The value of myPet is inserted into the template literal.
    • When we interpolate `I own a pet ${myPet}.`, the output we print is the string: 'I own a pet armadillo.'

    One of the biggest benefits to using template literals is the readability of the code. Using template literals, you can more easily tell what the new string will be. You also don’t have to worry about escaping double quotes or single quotes.

    'Study > JavaScript' 카테고리의 다른 글

    Control flow  (0) 2020.04.29
    The switch keyword  (0) 2020.04.29
    Create a Variable: let  (0) 2020.04.29
    Create a Variable: var  (0) 2020.04.29
    Properties  (0) 2020.04.28

    댓글

Designed by Tistory.