2015년 8월 30일 일요일

Javascript Expression and Operators

Let's see Javascript expression and operator. You can learn the basic content from MDN's  Expressions and Operators . We'll see Javascript specific things and something is good to know. Learning test is written using Jasmine. Please refer Jasmine usage which I wrote before if you have interest it. 

1. equal, strict equal
If the type is different like [line5], Javascript try to converting type automatically since javascript is dynamic typing language. If you want to check equality with the actual type, you can use strict equal operator like [line 7].
it("equals, strict equal", function() {
var num1 = 1;
expect(num1 == 1).toBe(true);
expect(num1 == "1").toBe(true);
expect(num1 === 1).toBe(true);
expect(num1 === "1").toBe(false);
var str1 = "1";
expect(str1 != 1).toBe(false);
expect(str1 != "1").toBe(false);
expect(str1 !== 1).toBe(true);
expect(str1 !== "1").toBe(false);
});
2. negation, plus
The plus(+) operator convert to number.
it("negation, plus operator", function() {
var num1 = 1;
expect(-num1).toBe(-1);
var str1 = "1";
expect(+str1).toBe(1);
});
3. string operator
it("string opertor", function() {
var str1 = "hi";
expect(str1 + " hello").toBe("hi hello");
});
4. delete operator
The "delete" operator delete specific property of object or specific index of array. The deleted object would be undefined like [line 9, line 15].
it("delete operator", function() {
var phone = {
"number" : "123-45678",
"owner" : "thinkhard.j.park"
}
expect(phone.owner).toBe("thinkhard.j.park");
delete phone.owner;
expect(phone.owner).toBe(undefined);
var aray = [0,1,2,3,4];
expect(aray[0]).toBe(0);
delete aray[0];
expect(aray[0]).toBe(undefined);
expect(0 in aray).toBe(false);
})
5. typeof
You can know the type using "typeof" operator.
it("typeof", function() {
var myFun = new Function("5 + 2");
var shape = "round";
var size = 1;
var today = new Date();
expect(typeof myFun).toBe("function");
expect(typeof shape).toBe("string");
expect(typeof size).toBe("number");
expect(typeof today).toBe("object");
expect(typeof notdefined).toBe("undefined");
expect(typeof null).toBe("object");
expect(typeof undefined).toBe("undefined");
});
6. void(expression)
The "void" operator evaluates expression like [line 5]. But don't return anything like [line6].
it("void(expression) : void evaluate expression but not return.", function() {
var myFun = function(a, b) {return a + b};
var result = 0;
expect(void(result = myFun(2, 5))).toBe(undefined);
expect(result).toBe(7);
});
7. in operator
The "in" operator check whether specific element is contained in Object or Array.
it("in operator return true if the specified property is in the specified object", function() {
var trees = ["redwood", "bay", "cedar", "oak", "maple"];
expect(0 in trees).toBe(true);
expect(5 in trees).toBe(false);
//should use index number
expect("bay" in trees).toBe(false);
expect("PI" in Math).toBe(true);
var myString = new String("coral");
expect("length" in myString).toBe(true);
var mycar = { make: "Honda", model: "Accord", year: 1998 };
expect("make" in mycar).toBe(true);
});

8. Reference
- MDN - https://developer.mozilla.org/en/docs/Web/JavaScript
- Jasmine - http://jasmine.github.io/2.3/introduction.html


댓글 없음:

댓글 쓰기