Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Javascript have something like %d?

Here is something I used in NSString ...

[NSString stringWithFormat:@"This is a digit %d", 10];

the 10 value will go to %d...., and the string will become "This is a digit 10", is there any similar thing in javascript? Thank you... Also, I would like to know, what is this call??

like image 687
Tattat Avatar asked Apr 15 '11 15:04

Tattat


People also ask

What is ${} in JavaScript?

${} is a placeholder that is used in template literals. You can use any valid JavaScript expression such as variable, arithmetic operation, function call, and others inside ${}. The expression used inside ${} is executed at runtime, and its output is passed as a string to template literals.

Can you use == to compare strings in JavaScript?

Explanation of the example: In the above example, when str1 and str2 are compared after using the toUpperCase method, the expression JAVASCRIPT == JAVASCRIPT would return true as they are the same strings after both being in upper case. Here, the equality operator (==) is used to check if both the strings are the same.

What is string in JavaScript?

A string is a sequence of one or more characters that may consist of letters, numbers, or symbols. Strings in JavaScript are primitive data types and immutable, which means they are unchanging.

How do you check if a string is equal to another string in JavaScript?

To check if two strings are equal in JavaScript, use equal-to operator == and pass the two strings as operands. The equal-to operator returns a boolean value of true if the two strings are equal, or else, it returns false.


2 Answers

There is no built-in string formatting, but you can use a JavaScript library to do the same: sprintf().

like image 76
Matt Ball Avatar answered Sep 21 '22 18:09

Matt Ball


You can concatenate strings easily in Javascript:

  var str = "This is a digit " + 10;
like image 29
Dongsheng Cai Avatar answered Sep 20 '22 18:09

Dongsheng Cai