Expanding ${var} references in a JavaScript string [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Learning something beyond vanilla JavaScript and the book I'm reading is telling me a statement like:
let someVar = 'Happy';
console.log('I hope you have a ${someVar} day.');
Should display 'Happy'? in the log or an alert or possibly anywhere.
It doesn't work. I'm using FireFox Dev Ed and I just get a line with the entire:
${someVar}
in it. Any guidance... Is this a weird transpiler issue or ES6+ issue?

You need backticks surrounding the string there in order for the interpreter to properly interpret it as a template literal.
let someVar = 'Happy';
console.log(`I hope you have a ${someVar} day.`);
When you have a normal string, you can use single quotes ' or double quotes ", but when you're using a template literal, you must always use backticks `.
You can also use backticks anyway even if you aren't interpolating any variables inside, just so you don't have to escape quote characters, for example.

Related

Same function different MD5 hash results [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to calculate the hash of a string. And I found that the result changes depending on the method I use.
In this webpage https://codebeautify.org/md5-hash-generator.
If I use the form writing the string in the text field:
16120&{"number":"4545","params":"{\"locale\":\"en_EN\"}"}
I get 26528d6e0e802d5569e2e03fde0a825c. However if I do
CryptoJS.MD5('16120&{"number":"4545","params":"{\"locale\":\"en_EN\"}"}').toString();
the result is fe31f378efcc4ae4de71e70278991741.
If I use a simple string like 1234 I get the same result but using the one above I don't so I guess the problem is escaped bars or something but I can't find a solution.
In your JS code, JS is escaping the quotes. The website is not escaping. So you are hashing different things, hence different hash results.
The Backslashes for escaping the string are only ok in-code because they will not be part of the resulting string. See w3schools -> chapter "Escape Character".
Try executing console.log('\"') to understand this, it results in logging only a single doublequote (").
So if you remove these escape characters and insert the string in the input (where escaping is neither needed nor supported), the hash will be equal.
The backslashes in the second case need to be doubled up. Once for JavaScript and once for JSON.
CryptoJS.MD5('16120&{"number":"4545","params":"{\\"locale\\":\\"en_EN\\"}"}').toString();

The nesting problem with “+=” (javascript) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Why is the result of JS -3?
a in JS does not seem to change with subsequent assignments
c:
int a=3;
a+=a-=a*a;
result:
a=-12;
js:
var a=3;
a+=a-=a*a;
result:
a=-3;
C and JavaScript have different rules to handle such expressions.
Clang (and probably also GCC and other C compilers) triggers a warning:
1.c:5:7: warning: unsequenced modification and access to 'a' [-Wunsequenced]
a+=a-=a*a;
~~ ^
1 warning generated.
In plain English this says that one of the read operations will not get the initial value of a but the current value of a at the moment when that operation is executed.
The statement above is executed the same way as:
a-=a*a;
a+=a;
This is why the result in C is -12 but it could be -3 as well.
Avoid writing such expressions. Even when there is no ambiguity about how they are evaluated, they are difficult to read and understand by other developers. One of the other developers is an older version of you. The code is written once but it is read many times. Let the code be easy to read an understand.

Javascript String.prototype unexpected token [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Apologies if this has been asked before but I couldn't find this specific issue.
I've been tasked with analysing a bit of highly obfuscated, and malicious, JS code. I'm trying to get it to run in a VM at the minute just to see exactly what it's up to but I'm getting a syntax error on the first line.
function String.prototype.x(){...
From Chrome's dev console:
Uncaught SyntaxError: Unexpected token .
The red squiggly line shows that the error is being thrown at the first dot (between String and prototype). To be honest, I don't know enough about JS to figure it out but I'm sure one of you lovely lot will know the answer. Why's it thrown and what can I do to fix it?
String is already a class (function with the inbuilt prototype methods) in JS , in order to create methods in its prototype chain you have use as below
String.prototype.reverse = function(){
return this.split('').reverse().join()
}
"hello".reverse() // olleh

Removing all characters after specific character [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I know this is a simple task, but my Javascript knowledge is (very) limited and I can't seem to get this to work. I've researched it thoroughly and have read through several similar questions here before posting, but still no luck. I'm trying to remove all characters after the comma within a particular div. My poor understanding of Javascript (and programming in general) is certainly limiting me here.
var s = document.getelementsbyclassname('menu-staff');
s = s.substring(0, s.indexOf(','));
document.write(s);
<div class="menu-staff">Value One, Value Two, Value Three, Value Four</div>
I can get this to work for a static string stored in the variable, so I know the Javascript function works, it must simply be a problem with targeting the div by class, right? Thanks for bearing with a newbie. I fully expect this to be flagged as a duplicate and removed in a matter of hours, but hopefully someone will be kind enough to guide an ignorant designer towards the light. :)
Split your string into parts and then just output the first one.
var str = document.querySelector('.menu-staff').innerHTML;
console.log(str);
var parts = str.split(',');
document.write(parts[0]); //Pssst... You shouldn't use document.write. Manipulate the DOm instead.
<div class="menu-staff">Value One, Value Two, Value Three, Value Four</div>
You could split pieces of your string separated by a comma into an array and just keep the first element :
var s = "bonjour, et bienvenu";
console.log(s.split(",")[0]); // bonjour

Javascript replace() to remove single quote produces weird result [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
With this code
"test\536".replace(/'/g, "")
I would expect there is no different to the original string, because there is no single quote. But I get this instead
"test+6"
When I run this on a string with single quote, it works as expected
"test'536".replace(/'/g, "")
"test536"
The problem is in your string, in JavaScript strings \ is used to escape the following character. so if you want to prevent this behavior you should escape it using another slash, it will be \\:
"test\\536".replace(/'/g, "")
console.log("test\\536".replace(/'/g, ""));
console.log("test'536".replace(/'/g, ""));
Hope this helps.

Categories