As mentioned in the previous section, the characters between quotes are called strings, regardless of odd and even quotes. Just as a variable can be a number, it can also be a string. Therefore, it can be said:
var nice_monkey = "The monkey smiles at you and recites Shakespeare.";
var bad_monkey = "The monkey scowls at you and burps.";
When declaring a variable, you assign a value to the variable so that it is equal to these strings. So when you want to write these strings, you can write:
(nice_monkey);
Here is an example of using strings to do things.
If you take a closer look at the source code, you will often find some new and interesting things.
See the following new things:
var monkey = prompt("What's the monkey's name?", "The monkey");
Here we call the user feedback prompt method. When it is called, a dialog box is launched to request the user to enter information. After the user completes, click OK to return the information. Return information in the uplink is put into its variable.
Note that there are two variables in this prompt method, and they are both strings. The first one is displayed on the input area of the dialog box. In this example it is: "What's the monkey's name?". In this example, the second parameter "The monkey" is used to place the default value of the input box. If you don't want the default, put quotes on the second parameter, just like this:
var monkey = prompt("What's the monkey's name?", "");
The next line is the direct variable allocation, just like we saw before:
var techy_monkey = monkey + demanding + tech;
This line introduces a string operator: accumulative tag. When an accumulation mark appears between two strings, the two variables appear in the same string, which is called "chain". So the upstream creates a new variable called techy_monkey containing a string containing the above three variables. In other words, the result is "The monkey" + "demands, no, insists upon receiving" + "a computer that won't crash, and a homemade browser!”
var techy_monkey = monkey + demanding + tech;
So it can also be said:
var techy_monkey = "The monkey demands, no, insists upon receiving a computer that won't crash, and a homemade browser!";
The following paragraph shows more tips for using strings. It works the same, we only look at three lines:
var italic_hippy = hippy_monkey.italics();
var shouting_hippy= hippy_monkey.toUpperCase();
var red_bold_tech = bold_tech.fontcolor('red');
The first line says: "Make the string contains variables displayed in italics", which is actually:
var italic_hippy = "<i>" + hippy_monkey + "</i>";
But it looks much better! In the future, when writing (italic_hippy) in JavaScript, you get characters displayed in italic.
The trick described in the next line cannot be implemented in HTML, which makes all characters in hippy_monkey appear in capitalization
The third line shows an example of changing the string attributes. . All characters have colors, you can use the ('new color'); command to change their colors. This can also be done:
var red_bold_tech = "<font color='red'>" + bold_tech + "</font>";
But it is not as easy as reading:
var red_bold_tech = bold_tech.fontcolor('red');
In this example, you can see other applications besides this bank:
(bold_tech + "<br>");
In addition to displaying a string instead, it also concatenates two strings and displays the result. It can also be written in two lines, like this:
var broken_bold = bold_tech + "<br>";
(broken_bold);
But this requires creating another variable, and it is unnecessary to write another line.
Now that we have learned all the knowledge about variables and strings, please do an exercise
<script language="JavaScript">
<!-- being hiding me
var name = prompt("Enter a subject: ","");
var verb = prompt("Enter a predicate: ","");
var adjective= prompt("Enter an object: ","");
var sentence = name + " " + verb + " " + adjective + "。<p>";
(sentence);
(());
(());
(('red'));
// end hiding me -->
</script>
When you finish the job, it's time to start practicing if clauses.
Previous page1234567Next pageRead the full text