Pryme8 Posted November 14, 2016 Share Posted November 14, 2016 So I have been programming with JavaScript for like oh gosh 18 some years now and am constantly looking for better methods. Last night I stumbled upon something I had never seen before and was kinda interested if it would really work because Dreamweaver was super angry about it so i figured it was out of convention. Anyways what I am referring to is literal strings... like you know how if you wanted to create a dynamic element so lets say a div or a textarea. Normally I thought to do this you would always need to just chain a string together so something like this: var block = document.createElement('div'); block.innerHTML = "Som stuff bllah elements"+ "New like of string stuff because who likes long strings"+ "by this point im annoyed..."; And that's is a pain like a super pain... and what happens when I want literals and dont want to to make extra spaces and the such, like for real im sure you can all agree it sucks. Anyways I discovered use that all time neglected `` you know the Gave Accent that no one ever uses... yeah well that trick little bugger makes JS ignore all formatting and calculate it as a literal string as far as I can tell so the inneHTML declaration ends up looking like this. block.innerHTML = `TIME SAVER! Especially if trying to show formated scripts The breaklines and tabs come through, and it ends up being way more readable and editable.`; like I know its very specific and I have not really tested it much... but I was like kinda stoked about it so I figured someone else might be too... Wingnut 1 Quote Link to comment Share on other sites More sharing options...
Kesshi Posted November 14, 2016 Share Posted November 14, 2016 You can do many nice things with these template literals. Look at this example: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Expression_interpolation Pryme8 and Wingnut 2 Quote Link to comment Share on other sites More sharing options...
Pryme8 Posted November 14, 2016 Author Share Posted November 14, 2016 Love it, so they are literals I was thinking so! man super cool. very excited. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.