Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I insert a variable in a string in .js, coming from a ruby example

In ruby you can insert variables into a string like this:

x = "sake"
puts "I like #{x}"
$"I like sake"

For example:

def what_i_like(word)
  "I like #{word}"
end 

Is there a similar way to do this in javascript?

What I'm doing now is this:

x = "sake"
"I like" + x + ". "
like image 466
thenengah Avatar asked Dec 14 '10 01:12

thenengah


People also ask

How do I pass a Ruby variable to JavaScript?

You can add in-line javascript in your view templates and interpolate ruby variables. HAML supports ruby interpolation under the :javascript tag with the #{} syntax. Using #{} by itself can result in errors.

How do you give a variable to a string in JavaScript?

You write the string as normal but for the variable you want to include in the string, you write the variable like this: ${variableName} . For the example above, the output will be the same as the example before it that uses concatenation.

How do you write a variable to a string in node JS?

To declare variables in JavaScript, you need to use the var, let, or const keyword. Whether it is a string or a number, use the var, let, or const keyword for its declaration. But for declaring a string variable we had to put the string inside double quotes or single quotes.


1 Answers

Check out this post: JavaScript equivalent to printf/string.format

like image 103
zsalzbank Avatar answered Nov 03 '22 23:11

zsalzbank