On Github SUNY-Albany-CCI-LearningEncounters / NodeJS-Introduction-Part-I
Created by Luis Ibanez
from a command line prompt
Simply type :
nodejs
Then hit the ENTER key.
Your prompt will now look like:
>
Let's try an addition:
> 2+2
Then hit the ENTER key.
and you will get
> 2+2 4
Let's create a variable:
> var fruit='apple'
Then hit the ENTER key.
Let's see it's value:
> fruit
Then hit the ENTER key.
and you will get
> fruit 'apple'
Let's change its value:
> var fruit='orange'
Then hit the ENTER key.
Let's see the new value:
> fruit
Then hit the ENTER key.
and you will get
> fruit 'orange'
"Variables"
are containers
that hold data.
the data in
"Variables"
can change.
by simply assigning
a new value to them
The value can be
a simple item...
Like a number
or a word
It can also be
a sentence...
Let's use a sentence as value:
> var book='The last of the Mohicans'
Then hit the ENTER key.
Let's see the value:
> book
Then hit the ENTER key.
and you will get
> book 'The last of the Mohicans'
Variables can also
hold a list of items...
Let's make a list:
> var groceries=['milk','eggs','bread']
Then hit the ENTER key.
Let's see the value:
> groceries
Then hit the ENTER key.
and you will get
> groceries [ 'milk', 'eggs', 'bread' ]
We use the
square brackets [ ]
to define a list
We use the
comma symbol ","
to separate multiple items
> var groceries=['milk','eggs','bread']
Variables can also
hold a list of properties...
Let's set some properties:
> var kermit={'species':'frog','color':'green'}
Then hit the ENTER key.
Let's see the value:
> kermit
Then hit the ENTER key.
and you will get
> kermit { species: 'frog', color: 'green' }
We use the
curly brackets { }
to define a set of properties
We use the
colon symbol ":"
to separate the property key from its value
var foo = { key : value }
(This is a generic example. Do not type it)
We use the
comma symbol ","
to separate multiple properties
var foo = { key1 : value1, key2 : value2 }
(This is a generic example. Do not type it)
More properties
can be added dynamically
Let's add more about kermit
> kermit.girlfriend = 'peggy'
Let's see the changes:
> kermit
Then hit the ENTER key.
and you will get
> kermit { species: 'frog', color: 'green', girlfriend: 'peggy' }
Let's add more about kermit
> kermit.work = 'sesame street'
Let's see the changes:
> kermit
Then hit the ENTER key.
You know a lot about me!
Variables can also
hold a hierarchy...
Think:Tree !
Think:Tree !
Let's go on a trip:
> var world={}
Then hit the ENTER key.
Let's add continents as properties:
> world.africa = {}
Then hit the ENTER key.
Let's see the changes:
> world
Then hit the ENTER key.
and you will get
> world { africa: {} }
Let's add more continents as properties:
> world.europe = {} > world.asia = {} > world.austrasia = {} > world.antartica = {} > world.northamerica = {}
Let's see the changes
> world { europe: {}, asia: {}, austrasia: {}, antartica: {}, northamerica: {} }
Let's add countries as properties:
> world.europe.france = {} > world.europe.england = {} > world.europe.germany = {} > world.europe.italy = {} > world.europe.poland = {}
Let's see the changes
> world { europe: { france: {}, england: {}, germany: {}, italy: {}, poland: {} }, asia: {}, austrasia: {}, antartica: {}, northamerica: {} }
We can also inspect a specific branch
> world.europe { france: {}, england: {}, germany: {}, italy: {}, poland: {} }
Pick a
Continent
Add three
countries
to it
Pick a
Country
Add three
cities
to it
Pick a
City
Add a list of three food items
that you would like to eat there
Inspect the variable
at the level of the city
It should look like
> world.southamerica.brazil { saopaulo: {}, riodejaneiro: { food: [ 'rodizio', 'mango', 'shrimp' ] }, salvador: {} }
Let's exit
nodejs
type
process.exit();
Now you should be back
at the '$' prompt
in the command line