Files
.github
api-server
client
config
curriculum
__fixtures__
challenges
_meta
arabic
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
access-array-data-with-indexes.english.md
access-multi-dimensional-arrays-with-indexes.english.md
accessing-nested-arrays.english.md
accessing-nested-objects.english.md
accessing-object-properties-with-bracket-notation.english.md
accessing-object-properties-with-dot-notation.english.md
accessing-object-properties-with-variables.english.md
add-new-properties-to-a-javascript-object.english.md
add-two-numbers-with-javascript.english.md
adding-a-default-option-in-switch-statements.english.md
appending-variables-to-strings.english.md
assigning-the-value-of-one-variable-to-another.english.md
assignment-with-a-returned-value.english.md
build-javascript-objects.english.md
chaining-if-else-statements.english.md
comment-your-javascript-code.english.md
comparison-with-the-equality-operator.english.md
comparison-with-the-greater-than-operator.english.md
comparison-with-the-greater-than-or-equal-to-operator.english.md
comparison-with-the-inequality-operator.english.md
comparison-with-the-less-than-operator.english.md
comparison-with-the-less-than-or-equal-to-operator.english.md
comparison-with-the-strict-equality-operator.english.md
comparison-with-the-strict-inequality-operator.english.md
comparisons-with-the-logical-and-operator.english.md
comparisons-with-the-logical-or-operator.english.md
compound-assignment-with-augmented-addition.english.md
compound-assignment-with-augmented-division.english.md
compound-assignment-with-augmented-multiplication.english.md
compound-assignment-with-augmented-subtraction.english.md
concatenating-strings-with-plus-operator.english.md
concatenating-strings-with-the-plus-equals-operator.english.md
constructing-strings-with-variables.english.md
count-backwards-with-a-for-loop.english.md
counting-cards.english.md
create-decimal-numbers-with-javascript.english.md
declare-javascript-variables.english.md
declare-string-variables.english.md
decrement-a-number-with-javascript.english.md
delete-properties-from-a-javascript-object.english.md
divide-one-decimal-by-another-with-javascript.english.md
divide-one-number-by-another-with-javascript.english.md
escape-sequences-in-strings.english.md
escaping-literal-quotes-in-strings.english.md
find-the-length-of-a-string.english.md
finding-a-remainder-in-javascript.english.md
generate-random-fractions-with-javascript.english.md
generate-random-whole-numbers-with-javascript.english.md
generate-random-whole-numbers-within-a-range.english.md
global-scope-and-functions.english.md
global-vs.-local-scope-in-functions.english.md
golf-code.english.md
increment-a-number-with-javascript.english.md
initializing-variables-with-the-assignment-operator.english.md
introducing-else-if-statements.english.md
introducing-else-statements.english.md
iterate-odd-numbers-with-a-for-loop.english.md
iterate-through-an-array-with-a-for-loop.english.md
iterate-with-javascript-do...while-loops.english.md
iterate-with-javascript-for-loops.english.md
iterate-with-javascript-while-loops.english.md
local-scope-and-functions.english.md
logical-order-in-if-else-statements.english.md
manipulate-arrays-with-pop.english.md
manipulate-arrays-with-push.english.md
manipulate-arrays-with-shift.english.md
manipulate-arrays-with-unshift.english.md
manipulating-complex-objects.english.md
modify-array-data-with-indexes.english.md
multiple-identical-options-in-switch-statements.english.md
multiply-two-decimals-with-javascript.english.md
multiply-two-numbers-with-javascript.english.md
nest-one-array-within-another-array.english.md
nesting-for-loops.english.md
passing-values-to-functions-with-arguments.english.md
practice-comparing-different-values.english.md
profile-lookup.english.md
quoting-strings-with-single-quotes.english.md
record-collection.english.md
replace-loops-using-recursion.english.md
replacing-if-else-chains-with-switch.english.md
return-a-value-from-a-function-with-return.english.md
return-early-pattern-for-functions.english.md
returning-boolean-values-from-functions.english.md
selecting-from-many-options-with-switch-statements.english.md
shopping-list.english.md
stand-in-line.english.md
store-multiple-values-in-one-variable-using-javascript-arrays.english.md
storing-values-with-the-assignment-operator.english.md
subtract-one-number-from-another-with-javascript.english.md
testing-objects-for-properties.english.md
understand-string-immutability.english.md
understanding-boolean-values.english.md
understanding-case-sensitivity-in-variables.english.md
understanding-undefined-value-returned-from-a-function.english.md
understanding-uninitialized-variables.english.md
updating-object-properties.english.md
use-bracket-notation-to-find-the-first-character-in-a-string.english.md
use-bracket-notation-to-find-the-last-character-in-a-string.english.md
use-bracket-notation-to-find-the-nth-character-in-a-string.english.md
use-bracket-notation-to-find-the-nth-to-last-character-in-a-string.english.md
use-conditional-logic-with-if-statements.english.md
use-multiple-conditional-ternary-operators.english.md
use-recursion-to-create-a-countdown.english.md
use-recursion-to-create-a-range-of-numbers.english.md
use-the-conditional-ternary-operator.english.md
use-the-parseint-function-with-a-radix.english.md
use-the-parseint-function.english.md
using-objects-for-lookups.english.md
word-blanks.english.md
write-reusable-javascript-with-functions.english.md
basic-javascript-rpg-game
debugging
es6
functional-programming
functional-programming-spreadsheet
intermediate-algorithm-scripting
intermediate-javascript-calorie-counter
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-quality-assurance
07-scientific-computing-with-python
08-data-analysis-with-python
09-information-security
10-coding-interview-prep
11-machine-learning-with-python
12-certificates
portuguese
russian
spanish
schema
test
.babelrc
.editorconfig
.npmignore
.travis.yml
CHANGELOG.md
LICENSE.md
comment-dictionary.js
commitizen.config.js
commitlint.config.js
create-challenge-bundle.js
getChallenges.acceptance.test.js
getChallenges.js
getChallenges.test.js
gulpfile.js
lib.js
package-entry.js
package-lock.json
package.json
utils.js
cypress
docs
tools
utils
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.npmrc
.prettierignore
.prettierrc
.snyk
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
HoF.md
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
crowdin.yml
cypress-install.js
cypress.json
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
jest.config.js
lerna.json
lighthouserc.js
package-lock.json
package.json
sample.env
freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/understanding-uninitialized-variables.english.md

82 lines
2.1 KiB
Markdown
Raw Normal View History

---
id: 56533eb9ac21ba0edf2244aa
title: Understanding Uninitialized Variables
challengeType: 1
videoUrl: 'https://scrimba.com/c/cBa2JAL'
forumTopicId: 18335
---
## Description
<section id='description'>
When JavaScript variables are declared, they have an initial value of <code>undefined</code>. If you do a mathematical operation on an <code>undefined</code> variable your result will be <code>NaN</code> which means <dfn>"Not a Number"</dfn>. If you concatenate a string with an <code>undefined</code> variable, you will get a literal <dfn>string</dfn> of <code>"undefined"</code>.
</section>
## Instructions
<section id='instructions'>
Initialize the three variables <code>a</code>, <code>b</code>, and <code>c</code> with <code>5</code>, <code>10</code>, and <code>"I am a"</code> respectively so that they will not be <code>undefined</code>.
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: <code>a</code> should be defined and evaluated to have the value of <code>6</code>.
testString: assert(typeof a === 'number' && a === 6);
- text: <code>b</code> should be defined and evaluated to have the value of <code>15</code>.
testString: assert(typeof b === 'number' && b === 15);
- text: <code>c</code> should not contain <code>undefined</code> and should have a value of "I am a String!"
testString: assert(!/undefined/.test(c) && c === "I am a String!");
- text: You should not change code below the specified comment.
testString: assert(/a = a \+ 1;/.test(code) && /b = b \+ 5;/.test(code) && /c = c \+ " String!";/.test(code));
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
// Only change code below this line
var a;
var b;
var c;
// Only change code above this line
a = a + 1;
b = b + 5;
c = c + " String!";
```
</div>
### After Test
<div id='js-teardown'>
```js
(function(a,b,c){ return "a = " + a + ", b = " + b + ", c = '" + c + "'"; })(a,b,c);
```
</div>
</section>
## Solution
<section id='solution'>
```js
var a = 5;
var b = 10;
var c = "I am a";
a = a + 1;
b = b + 5;
c = c + " String!";
```
</section>