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].
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
The plus(+) operator convert to number.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it("negation, plus operator", function() { | |
var num1 = 1; | |
expect(-num1).toBe(-1); | |
var str1 = "1"; | |
expect(+str1).toBe(1); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
it("string opertor", function() { | |
var str1 = "hi"; | |
expect(str1 + " hello").toBe("hi hello"); | |
}); |
The "delete" operator delete specific property of object or specific index of array. The deleted object would be undefined like [line 9, line 15].
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}) |
You can know the type using "typeof" operator.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
}); |
The "void" operator evaluates expression like [line 5]. But don't return anything like [line6].
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
The "in" operator check whether specific element is contained in Object or Array.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
댓글 없음:
댓글 쓰기