- About Javascript
- Javascript Basics
- Strings, Arrays and Objects
- Control Structures
- Functions, Scope and Closures
- Object Functions, Constructors and Prototypes
- Javascript Next
Example code based on
LearnXinYminutes
and licensed under
CC Attribution-Share 3
Strings, Arrays and Objects
Strings
The Javascript String is immutable, but it includes various accessor methods such as substring and indexOf.
// Strings are created with ' or ".
'abc';
"Hello, world";
// Strings are concatenated with +
"Hello " + "world!"; // = "Hello world!"
// Other types automatically convert to strings for concatenation
"Hello " + 10; // = "Hello 10"
// Strings can be alphabetically compared with < and >
"a" < "b"; // = true
// Access specific characters with charAt
"Hello".charAt(0); // = "H"
//...or use substring to get larger pieces
"Hello world".substring(0, 5); // = "Hello"
//length is a property, so don't use ()
"Hello".length; // = 5
//Print to the console (for debugging)
console.log("Hello"); // Hello
// Print to output in these challenges
print("hello"); // Hello
Arrays
Arrays are ordered lists of values of any type. They share many methods with Strings.
var myArray = ["Hello", 42, true];
// Their members can be accessed using the square-brackets subscript syntax.
// Array indices start at zero.
myArray[1]; // = 42
// Use push to add to an Array
myArray.push("World");
//or Add/modify at a specific index:
myArray[1] = 2;
myArray; // = ["Hello", 2, true, "World"]
myArray.length; // = 4
Objects / Hashes
A JavaScript object is an unordered collection of unique keys mapped to specific values. In this way, they're similar to the Hashes or dictionaries of other languages.
var myObj = {key1: "Hello", key2: "World"};
// Keys are strings, but quotes aren't required if they're a valid
// JavaScript identifier. Values can be any type.
var myObj = {myKey: "myValue", "another key": 20};
// Object attributes can be accessed using the subscript syntax,
myObj["another key"]; // = 20
// ... or using the dot syntax, provided the key is a valid identifier.
myObj.myKey; // = "myValue"
// Objects are mutable; values can be changed and new keys added.
myObj.myThirdKey = true;
myObj["key4"] = 40;
myObj; // = Object {myKey: "myValue", another key: 20, myThirdKey: true, key4: 40}
// Accessing a key that's not there will return undefined
myObj.key5; // = undefined
Javascript objects can also have associated functions, which will be covered soon.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Challenge
For each word
, you are given, print "Hello {word}!".
(Replace {word} with the given word).
Note: In these challenges, use print()
to print to output.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Challenge
Using the same syntax as above, create a car
object with a "green" color
and a speed
of 0.
(Remember to use var
and a ;
at the end)
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
Dab Bol
Feb 26, 8:48 AMFor each word, you are given, print "Hello {word}!". <- this is a function challenge, not an object challenge!