Variables
1. Basic JavaScript
-
How to comment?
//exactitude /* hi, how is the novel?*/
2. Seven Data types
- undefined: declared variables without values
- null
- symbol
- boolean
- string: immutable
- number
- object
3. Numeric variable
-
Store a value in a variable
var q = 1.1
-
Value Operator
-
Increment and decrement
i = 19 i++ // is the equivalent of i = i + 1 i-- // is the equivalent of i = i - 1
-
Remainder operator
%
17 % 2 = 1 // 17 is odd 18 % 2 = 0 // 18 is even
-
Compound assignment With augmented addition, substraction, multiplication and devision
var a = 90; a += 9; // this is equal to a = a + 9; a -= 9; // this is equal to a = a -9; a *= 9; // this is eqaul to a = a * 9; a /= 9; // this is equal to a = a / 9;
-
4. String variable
var p = "Mauriat";
# Escaping Literal Quotes in Strings
var p = "Mauriat said, \"I worked as an arranger for other artists \".";
# Quoting Strings with Single Quotes
var myStr = '<a href="http://www.example.com" target="_blank">Link</a>';
# Escape Sequences in Strings
var myStr = 'FirstLine\n\t\\SecondLine\nThirdLine'; // Change this line
- Escaping characters
Code | Output |
---|---|
-' | single quote |
-" | double quote |
-\ | backslash |
-\n | newline |
-\r | carriage return |
-\t | tab |
-\b | word boundary |
-\f | form feed |
- Concatenating Strings with plus operator, plus equals operator
var myWords = "I hear " + "you say: ";
var myWords += "It's gonna be OK";
- Find the lenth of a String
var myWordseLength = myWords.length;
- Use bracket notation to find the first/last character in a string
var firstLetter = myWords[0];
var lastLetter = myWords[myWords.length-1];
var thirdToLastLetter = myWords[myWords.length - 3];
5. Array variable
- Store multiple values in one variable using JavaScript arrays
var myArray = ["a",1]
- Multidimensional array: nest one array within another array
var myArray = [["a",1],["b",2]]
- Access array data with indexes
var myData = myArray[0]
# Access array of arrays
var arr = [
[1,2,3],
[4,5,6],
[7,8,9],
[[10,11,12], 13, 14]
];
arr[3]; // equals [[10,11,12], 13, 14]
arr[3][0]; // equals [10,11,12]
arr[3][0][1]; // equals 11
- Modify array data with indexes
var ourArray = [50,40,30];
ourArray[0] = 15; // equals [15,40,30]
- Manipulate arrays with push()
var arr1 = [1,2,3];
arr1.push(4);
// arr1 is now [1,2,3,4]
var arr2 = ["Stimpson", "J", "cat"];
arr2.push(["happy", "joy"]);
// arr2 now equals ["Stimpson", "J", "cat", ["happy", "joy"]]
- Remove the first element from an array
var ourArray = ["Stimpson", "J", ["cat"]];
var removedFromOurArray = ourArray.shift();
// removedFromOurArray now equals "Stimpson" and ourArray now equals ["J", ["cat"]]
- Remove the last element from an array
var threeArr = [1, 4, 6];
var oneDown = threeArr.pop();
console.log(oneDown); // Returns 6
console.log(threeArr); // Returns [1, 4]
- Add an element to the beginning of an array
var ourArray = ["Stimpson", "dog"];
ourArray.shift(); // ourArray now equals ["dog"]
ourArray.unshift("Happy");
// ourArray now equals ["Happy", "dog"]
6. Boolean
Boolean values are never written with quotes.
The strings “true” and “false” are not Boolean and have no special meaning in JavaScript.