What is output and why?
(function(){
var a = 3;
b = 3;
})();
console.log("a defined? " + (typeof a !== 'undefined'));
console.log("b defined? " + (typeof b !== 'undefined'));
false
True
Is this same result? if not, what is best practice?
function foo1()
{
return {
bar: "hello"
};
}
function foo2()
{
return
{
bar: "hello"
};
}
(function() {
console.log(1);
setTimeout(function(){console.log(2)}, 1000);
setTimeout(function(){console.log(3)}, 0);
console.log(4);
})();
What is the order?
var test = "3";
(function outerFunc(test) {
var test = '4';
(function innerFunc(test) {
console.log(test);
})(2);
})(1);
What is output?
console.log(false == '0')
console.log(false === '0')
What is the output? explain.
true false
var text = 'outside';
function logIt(){
console.log(text);
var text = 'inside';
};
logIt();
What is the output?
if (true) {
var value = 'test';
}
console.log(value);
What is output?
var value = 1;
function Foo(value){
value += 100;
}
Foo(value);
console.log(value);
What is output?
<button id="btn0">Button 1!</button>
<button id="btn1">Button 2!</button>
<script type="text/javascript">
var values = ['test1', 'test2'];
for (var btnNum = 0; btnNum < values.length; btnNum++) {
document.getElementById('btn' + btnNum).onclick = function() {
alert(values[btnNum]);
};
}
</script>
This will give us all undefined value when pressing button. What is wrong with this?
Use wrapper function.
<button id="btn0">Button 1!</button>
<button id="btn1">Button 2!</button>
<script type="text/javascript">
var values = ['test1', 'test2'];
for (var btnNum = 0; btnNum < values.length; btnNum++) {
document.getElementById('btn' + btnNum).onclick = function(frozenBtnNum){
return function() {
alert(values[frozenBtnNum]);
};
}(btnNum);
}
</script>