简体   繁体   中英

How to add double forward slash in a string without commenting out in Javascript?

I would like to create a string but when I create the string variable the double forward slash comments out the rest of the line. How do I create a string without commenting out part of the string?

example:

var linkVariable = "http://free.clipartof.com/176-Free-Cartoon-Owl-Clipart.jpg"

Everything after the // gets commented out.

you create a string in javascript by putting the string inside quotations:

var answer = "It's alright";
var answer = "He is called 'Johnny'";
var answer = 'He is called "Johnny"';

Which you have correctly done.

What you are seeing is likely syntax highlighting from your code editor/IDE.

A good tool, as you are getting started, is jsfiddle.

I have set up a jsFiddle for you with the following code:

 var linkVariable = "http://free.clipartof.com/176-Free-Cartoon-Owl-Clipart.jpg" document.getElementById('link').value = linkVariable;
 <input style="width:300px;" type="text" id="link"></input>

also available at

http://jsfiddle.net/chrislewispac/yte9f5yv/

Where you can see the value of your string variable in an input box.

Go take a look and play around creating the different types in javascript. I've learned a lot from isolating problems and testing it in an external environment!

Also, your code editor probably has settings that can be adjusted to fix this for you.

Edit: (since you are using Sublime Text) In sublime text you can go to Preferences -> Browse Packages -> Javascript and find the file Comments.tmPreferences . You can then edit your comments preferences.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM