Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Storing and Tracking Information with Variables Combining Strings

Create a variable named fullName that combines both the first and last name variables to create a string like "Mary Jone

I have tried two different codes they are both not working see bellow

var firstName = "virginia "; var lastName = "CHUBO"; var firstName, lastName, fullName; firstName = 'virginia'; lastName = 'CHUBO'; fullName = 'firstName' + 'lastName';

2

var firstName = 'virginia'; var lastName = 'chubo'; fullName = firstName + lastName

script.js
var firstName = "virginia ";
var lastName = "CHUBO";
var firstName, lastName, fullName;
firstName = 'virginia';
lastName = 'CHUBO';
fullName = 'firstName' + 'lastName';
index.html
<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JavaScript Basics</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

4 Answers

Julian Gutierrez
Julian Gutierrez
19,201 Points

It seems like you're close on one of your attempts. The + character is used to combine strings but you also need to include the blank space as shown below.

var firstName = "virginia";
var lastName = "Chubo";
var fullName = firstName + " " + lastName;

You have a couple mistakes. The biggest one is that you are not actually referencing your firstName and lastName variables when assigning a value to fullName. You are actually just setting its value as the names of those variables combined. See below:

var firstName = 'virginia';
var lastName = 'CHUBO';
var fullName = 'firstName' + 'lastName';    // value is 'firstNamelastName'

When referencing variables don't use quotes!

The second problem is that when you are combining the string values you are not including a space between the names. Below is an example that will pass the code challenge.

var firstName = 'Erik';
var lastName = 'Krieg';
var fullName = firstName + ' ' + lastName;    // value is 'Erik Krieg'

I hope this helped!

Tatyana Vankova
Tatyana Vankova
5,003 Points

var fullName=prompt; fullName="firstName+lastName";

Alex Gervais
Alex Gervais
5,290 Points

The 3rd line in your code above does nothing but redeclare the variables firstName & lastName and declare an empty variable called fullName. You're close with the last 3 lines, the only change you have to make is to the fullName declaration:

var firstName = 'virginia';
var lastName = 'CHUBO';
var fullName = firstName + lastName;

You're trying to access the firstName and lastName variables, but since you have single quotes around them they are read as strings.