Khoerodin Developer, writer, and maker |

Developer, writer, and maker

ES6 String Interpolation

Jika sudah terbiasa dengan Ruby, sangat mudah sekali melakukan interpolasi string seperti ini:

middle_name = 'Salwa'
"Hi, my name is #{middle_name}!"

Sedangkan untuk javascript seperti ini:

middleName = 'Salwa'
"Hi, my name is " +  name + "!"

atau

var myClass = "custom-class"
var markup = "<p class=\"" + myClass + "\">Paragraph</p>"

atau

var welcome = "Hello " + (isWorld ? "World" : "me")

Jika multiline:

var firtName = 'Aisha'
var middleName = 'Salwa'
var lastName = 'Faqeeha'

var greeting = "Hi, my name is " +  middleName + "!, and my full " +
"name is " + firstName + middleName + lastName

Bagaimana jika ES6? tadaaaa….. jeng jeng jeng….

let myClass = "custom-class"
let markup = `<p class="${myClass}">Paragraph</p>`

atau

let welcome = `Hello ${isWorld ? "World" : "me"}!`

Jika multiline:

let firtName = 'Aisha'
let middleName = 'Salwa'
let lastName = 'Faqeeha'

let greeting = `Hi, my name is ${middleName}!, and my full
name is ${firstName} ${middleName} ${lastName}`

Mudah kan, mudahh kan? :grinning: