Tag Archives: comment

JavaScript Learning Class 101

A quick guide to JavaScript grammar:

http://www.codecademy.com/en/glossary/javascript

An excellent online JavaScript Interpreter, where you can directly write, test and share your JavaScript code:

http://repl.it/

1. How to get the length of the string “cake”?

“cake”.length  (not “cake”.length())

2. How to comment in JavaScript?

Using // before a line to comment this line, just like C++ and Java. For MATLAB and Latex, you should use %.

3. What are the comparison operators for “Equal to ” and “Not Equal to”?

Equal to ===

Not Equal to !==

This is very different from other programming languages.

A simple explanation would be that == does just value checking ( no type checking ) , whereas , === does both value checking and type checking . Seeing the examples may make it all clear. It is always advisable that you never use == , because == often produces unwanted results

Syntax
expression == expression
expression === expression
Example
'1' == 1 //true (same value)
'1' === 1 // false  (not the same type)

true == 1 // true (because 1 stands for true ,though it's not the same type)
true === 1 // false (not the same type)

4. How to print out things?

Using console.log(string), for example, console.log(“I am right”);

5. How to obtain the substring of a string?

“some string”.substring(x, y) gives the substring starting from x and finishing at y-1. The index of a string starts from 0. For example, “hello”.substring(0,2) returns “he”.

6. How to declare a variable?

Using var. Note that in JavaScript, all types of variables are declared using var.

var a = 2;

var b = “hello”;

7. How to get an input value from the user?

Use prompt. For example, var input = prompt(“Input a number”);

8. How to declare a function called mulBy2?

There are two ways to declare a function.

function mulBy2(number) {

    return number * 2;

}

var mulBy2 = function(number) {

    return number * 2;

};

9. How to get the type of a variable?

Use typeof. For example, var temp = prompt(“input your age”); typeof(temp);

10. How to create a two dimensional array?

var twoDimArray = [[1, 2, 3], [4, 5, 6]]; //create a 2×3 two dimensional array

var twoDimJagArray = [[1], [2,3]];//the first row has 1 element, the second row has 2 elements

tips: we can create a array containing different data types.

var array = [1, ‘a’, [1, 2]];

11. How to create an object?

var obj = {                                        var obj2 = new Object();

  key1: value1,                                    obj2.key1 = value1;

  key2: value2,                                    obj2.key2 = value2;

  key3:value3                                      obj2.key3 = value3;

};

var obj3 = {

  key1: value1,

  key2: {

  key21: value21,

  key22: value22

},

  key3:value3

};

12. How to create a class?

function Rectangle(height, width) {
    this.height = height;
    this.width = width;
    this.calcArea = function() {
    return this.height * this.width;
    };
// put our perimeter function here!
    this.calcPerimeter = function() {
    return 2 * (height + width);
    }
}

var rex = new Rectangle(7,3);
var area = rex.calcArea();
var perimeter = rex.calcPerimeter();