SHARE
Facebook X Pinterest WhatsApp

Using JavaScript Variables and Built-In Functions, Part 2

Dec 18, 2006

Excerpted from Chapter 2, JavaScript 2nd Edition: A Practical Guide To Interactive Web Pages from No Starch Press


Strings

Any series of characters between quotes is called a string. (You’ll be seeing lots of strings throughout this book.) Strings are a basic type of information, like numbers–and like numbers, you can assign them to variables.


To assign a string to a variable, you’d write something like this:

var my_name = “thau!”;

The word thau! is the string assigned to the variable my_name.



You can stick strings together with a plus sign (+), as shown in the bolded section of Figure 2-6. This code demonstrates how to write output to your page using strings.

<html><head>
<title>Seconds in a Day</title>
<script type = “text/javascript”>
<!– hide me from older browsers
var seconds_per_minute = 60;
var minutes_per_hour = 60;
var hours_per_day = 24;
var seconds_per_day = seconds_per_minute * minutes_per_hour * hours_per_day;
// show me –></script>
</head><body>
<h1>My calculations show that . . .</h1>
<script type = “text/javascript”>
<!– hide me from older browsers
X var first_part = “there are “;
Y var last_part = ” seconds in a day.”;
Z var whole_thing = first_part + seconds_per_day + last_part;
window.document.write(whole_thing);
// show me –>
</script>
</body>
</html>

Figure 2-6: Putting strings together

Line-by-Line Analysis of Figure 2-6

Line X in Figure 2-6,

var first_part = “there are “;

assigns the string

there are

to the variable first_part. Line Y,


var last_part = ” seconds in a day.”;

sets the variable last_part to the string

seconds in a day.

Line Z glues together the values stored in first_part, seconds_per_day, and last_part. The end result is that the variable whole_thing includes the whole string you want to print to the page, there are 86400 seconds in a day. The window.document.write() line then writes whole_thing to the web page.



NOTE: The methods shown in Figures 2-4 and 2-6 are equally acceptable ways of writing there are 86400 seconds in a day. However, there are times when storing strings in variables and then assembling them with the plus sign (+) is clearly the best way to go. We’ll see a case of this when we finally get to putting the date on a page.

Recommended for you...

The Revolutionary ES6 Rest and Spread Operators
Rob Gravelle
Aug 23, 2022
Ahead of Time (AOT) Compilation in Angular
Tariq Siddiqui
Aug 16, 2022
Converting a JavaScript Object to a String
Rob Gravelle
Aug 14, 2022
Understanding Primitive Type Coercion in JavaScript
Rob Gravelle
Jul 28, 2022
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.