Files
.github
api-server
client
config
curriculum
__fixtures__
challenges
_meta
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.md
access-multi-dimensional-arrays-with-indexes.md
accessing-nested-arrays.md
accessing-nested-objects.md
accessing-object-properties-with-bracket-notation.md
accessing-object-properties-with-dot-notation.md
accessing-object-properties-with-variables.md
add-new-properties-to-a-javascript-object.md
add-two-numbers-with-javascript.md
adding-a-default-option-in-switch-statements.md
appending-variables-to-strings.md
assigning-the-value-of-one-variable-to-another.md
assignment-with-a-returned-value.md
build-javascript-objects.md
chaining-if-else-statements.md
comment-your-javascript-code.md
comparison-with-the-equality-operator.md
comparison-with-the-greater-than-operator.md
comparison-with-the-greater-than-or-equal-to-operator.md
comparison-with-the-inequality-operator.md
comparison-with-the-less-than-operator.md
comparison-with-the-less-than-or-equal-to-operator.md
comparison-with-the-strict-equality-operator.md
comparison-with-the-strict-inequality-operator.md
comparisons-with-the-logical-and-operator.md
comparisons-with-the-logical-or-operator.md
compound-assignment-with-augmented-addition.md
compound-assignment-with-augmented-division.md
compound-assignment-with-augmented-multiplication.md
compound-assignment-with-augmented-subtraction.md
concatenating-strings-with-plus-operator.md
concatenating-strings-with-the-plus-equals-operator.md
constructing-strings-with-variables.md
count-backwards-with-a-for-loop.md
counting-cards.md
create-decimal-numbers-with-javascript.md
declare-javascript-variables.md
declare-string-variables.md
decrement-a-number-with-javascript.md
delete-properties-from-a-javascript-object.md
divide-one-decimal-by-another-with-javascript.md
divide-one-number-by-another-with-javascript.md
escape-sequences-in-strings.md
escaping-literal-quotes-in-strings.md
find-the-length-of-a-string.md
finding-a-remainder-in-javascript.md
generate-random-fractions-with-javascript.md
generate-random-whole-numbers-with-javascript.md
generate-random-whole-numbers-within-a-range.md
global-scope-and-functions.md
global-vs.-local-scope-in-functions.md
golf-code.md
increment-a-number-with-javascript.md
initializing-variables-with-the-assignment-operator.md
introducing-else-if-statements.md
introducing-else-statements.md
iterate-odd-numbers-with-a-for-loop.md
iterate-through-an-array-with-a-for-loop.md
iterate-with-javascript-do...while-loops.md
iterate-with-javascript-for-loops.md
iterate-with-javascript-while-loops.md
local-scope-and-functions.md
logical-order-in-if-else-statements.md
manipulate-arrays-with-pop.md
manipulate-arrays-with-push.md
manipulate-arrays-with-shift.md
manipulate-arrays-with-unshift.md
manipulating-complex-objects.md
modify-array-data-with-indexes.md
multiple-identical-options-in-switch-statements.md
multiply-two-decimals-with-javascript.md
multiply-two-numbers-with-javascript.md
nest-one-array-within-another-array.md
nesting-for-loops.md
passing-values-to-functions-with-arguments.md
practice-comparing-different-values.md
profile-lookup.md
quoting-strings-with-single-quotes.md
record-collection.md
replace-loops-using-recursion.md
replacing-if-else-chains-with-switch.md
return-a-value-from-a-function-with-return.md
return-early-pattern-for-functions.md
returning-boolean-values-from-functions.md
selecting-from-many-options-with-switch-statements.md
shopping-list.md
stand-in-line.md
store-multiple-values-in-one-variable-using-javascript-arrays.md
storing-values-with-the-assignment-operator.md
subtract-one-number-from-another-with-javascript.md
testing-objects-for-properties.md
understand-string-immutability.md
understanding-boolean-values.md
understanding-case-sensitivity-in-variables.md
understanding-undefined-value-returned-from-a-function.md
understanding-uninitialized-variables.md
updating-object-properties.md
use-bracket-notation-to-find-the-first-character-in-a-string.md
use-bracket-notation-to-find-the-last-character-in-a-string.md
use-bracket-notation-to-find-the-nth-character-in-a-string.md
use-bracket-notation-to-find-the-nth-to-last-character-in-a-string.md
use-conditional-logic-with-if-statements.md
use-multiple-conditional-ternary-operators.md
use-recursion-to-create-a-countdown.md
use-recursion-to-create-a-range-of-numbers.md
use-the-conditional-ternary-operator.md
use-the-parseint-function-with-a-radix.md
use-the-parseint-function.md
using-objects-for-lookups.md
word-blanks.md
write-reusable-javascript-with-functions.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
dictionaries
schema
test
.babelrc
LICENSE.md
create-challenge-bundle.js
crowdin.yml
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
HoF.md
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
cypress-install.js
cypress.json
jest.config.js
lerna.json
lighthouserc.js
package-lock.json
package.json
sample.env

96 lines
1.9 KiB
Markdown
Raw Normal View History

---
id: 56bbb991ad1ed5201cd392d1
title: Updating Object Properties
challengeType: 1
videoUrl: 'https://scrimba.com/c/c9yEJT4'
forumTopicId: 18336
---
## Description
<section id='description'>
After you've created a JavaScript object, you can update its properties at any time just like you would update any other variable. You can use either dot or bracket notation to update.
For example, let's look at <code>ourDog</code>:
```js
var ourDog = {
"name": "Camper",
"legs": 4,
"tails": 1,
"friends": ["everything!"]
};
```
Since he's a particularly happy dog, let's change his name to "Happy Camper". Here's how we update his object's name property:
<code>ourDog.name = "Happy Camper";</code> or
<code>ourDog["name"] = "Happy Camper";</code>
Now when we evaluate <code>ourDog.name</code>, instead of getting "Camper", we'll get his new name, "Happy Camper".
</section>
## Instructions
<section id='instructions'>
Update the <code>myDog</code> object's name property. Let's change her name from "Coder" to "Happy Coder". You can use either dot or bracket notation.
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: You should update <code>myDog</code>&apos;s <code>"name"</code> property to equal "Happy Coder".
testString: assert(/happy coder/gi.test(myDog.name));
- text: You should not edit the <code>myDog</code> definition.
testString: 'assert(/"name": "Coder"/.test(code));'
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
// Setup
var myDog = {
"name": "Coder",
"legs": 4,
"tails": 1,
"friends": ["freeCodeCamp Campers"]
};
// Only change code below this line
```
</div>
### After Test
<div id='js-teardown'>
```js
(function(z){return z;})(myDog);
```
</div>
</section>
## Solution
<section id='solution'>
```js
var myDog = {
"name": "Coder",
"legs": 4,
"tails": 1,
"friends": ["freeCodeCamp Campers"]
};
myDog.name = "Happy Coder";
```
</section>