JavaScript Quiz #13

Assume that we have the following short JavaScript code:

<script>
    var result = 3..valueOf() + (1, 2, 4);
    alert(result);
</script>

Will this code succeed or fail? and if it succeeds, what is the output of the alert?
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
The code will work fine. The final result is 7. Let’s understand why we will have this result. Let’s divide the expression in two parts. In the first part, we have:

3..valueOf()

This expression will work fine because in JavaScript, it is valid to trail decimal points in float numbers, so for example 3 can be represented as 3 or 3.0 or 3. (without having to mention the extra 0 after the decimal dot).
So the expression can be read simply as (3.).valueOf() which will return 3.

The second part is straightforward:

(1, 2, 4)

In order to know the result of this expression, we have to know how expressions work with the comma operator, if we have an expression that contains many comma operators then this expression will be evaluated to the last mentioned value. This means that
(1, 2, 4) will be evaluated to 4.

Adding the first part result to the second part result will result in: 3 + 4 = 7.

JavaScript Quiz #12

Assume that we have the following short JavaScript code:

<script>
    var number = 50;
    var obj = {
        number: 60,
        getNum: function () {
	    var number = 70;
	    return this.number;
	}
    }; 

    alert(obj.getNum());
    alert(obj.getNum.call());
    alert(obj.getNum.call({number:20}));
</script> 

Will this code succeed or fail? and if it succeeds, what is the output of the alerts?
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
The code will work fine. The alerts will output 60, 50 and 20. Let’s understand why we will have these results.

The sequence of operations:
1. In obj.getNum(), this.number will return number in obj scope which is 60.
2. In obj.getNum.call(), this.number will return number in global scope because Function.prototype.call‘s first parameter is not
specified which mean that the scope will be the global scope. So the result will be 50.
3. In obj.getNum.call({number:20}), this.number will return number in the object scope specified in the first parameter
of Function.prototype.call method which is {number: 20}. So the result will be 20.

More information about Function.prototype.call() can be found here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call

JavaScript Quiz #11

Assume that we have the following short JavaScript code:

<script>
    var x = !!"Hello" + (!"world", !!"from here!!");
    
    alert(x);
</script> 

Will this code succeed or fail? and if it succeeds, what is the output of the alert?
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
The code will work fine. The alert will output 2. Let’s understand why we will have this result.

The following sequence of operations occurs:
1. !!"Hello" ==> This operand will be evaluated to !false which will be finally true.
2. (!"world", !!"from here!!") ==> The comma operator evaluates both of its operands (from left to right) and returns the value of the second operand. which means that !!"from here!!" will be returned and will be evaluated to true.
3. As none of the operands is of String type, the + operands will be converted to number which means true + true will be evaluated to 1 + 1 which will equal finally 2.

JavaScript Quiz #8 (One line Quiz)

Assume that we have the following short JavaScript code:

<script>
    var result = (2..valueOf() + ({z : 10, x : 20}).x);
    alert(result);
</script>

Will this code succeed or fail? and if it succeeds, what is the output of the alert?
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
The code will work fine. The final result is 22. Let’s understand why we will have this result. Let’s divide the expression in two parts. In the first part, we have:

2..valueOf()

This expression will work fine because in JavaScript, it is valid to trail decimal points in float numbers, so for example 2 can be represented as 2 or 2.0 or 2. (without having to mention the extra 0 after the decimal dot).
So the expression can be read simply as (2.).valueOf() which will return 2.

The second part is straightforward:

({z : 10, x : 20}).x

We have here a JavaScript object with two attributes x and z, and we are getting the value of x using .x, which means that the result will be equal to x value which is 20.

Adding the first part result to the second part result will result in: 20 + 2 = 22.

JavaScript Quiz #7

Assume that we have the following short JavaScript code:

<script>
    var str = new String("Hello");
	
    var result = typeof(str instanceof String);
    alert(result); //What is the output of the alert? 
	
    result = typeof typeof(str instanceof String);
    alert(result); //What is the output of the alert? 
	
    result = typeof typeof typeof(str instanceof String);
    alert(result); //What is the output of the alert? 	
</script>

What is the output of each alert?
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
The result will be as follows boolean then string then string. Let’s understand why we have these results. In the first expression (which is a very straightforward):

var result = typeof(str instanceof String);

Will executed as follows:
1. str instanceof String will return true.
2. typeof (true) will return "boolean".

In the second expression:

var result = typeof typeof(str instanceof String);

Why does result will be string, this is because the expression will be executed as follows:
1. str instanceof String will return true.
2. typeof (true) will return "boolean" as you notice here typeof(true) returns a String that contains “boolean” value. It is important to know that JavaScript typeof operator always returns String.
3. Finally, it is now clear that typeof ("boolean") will return "string".

In the third expression:

var result = typeof typeof typeof(str instanceof String);

It is much similar to the second expression, result will return “string” because the third expression will be executed as follows:
1. str instanceof String will return true.
2. typeof (true) will return "boolean".
3. typeof ("boolean") will return "string".
3. Finally, typeof ("string") will return "string".

So now you can guess what is the result of ;):

alert(typeof typeof typeof typeof typeof typeof typeof(str instanceof String));

JavaScript Quiz #6

Assume that we have the following short JavaScript code:

<script>
	var result = [12 + 21]['valueOf']()['length'];
	alert(result); //What is the output of the alert?

	result = [12 + 21]['valueOf']['length'];
	alert(result); //What is the output of the alert? 
</script>

What is the output of each alert?
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
The result will be as follows 1 then 0. The first expression (the easy one) will be executed as follows:

var result = [12 + 21]['valueOf']()['length'];

The expression can be read as [33].valueOf().length which means that .length property will print the length of the valueOf() call to array [33] which is 1. Note that the [33].valueOf will return [33].

The second expression is the tricky part:

var result = [12 + 21]['valueOf']['length'];

The expression can be read as follows [33].valueOf.length. This is really completely different from the first expression because in this case the .length property will print the number of arguments of the valueOf method which is zero. This is because if the .length property is attached to a method then it will print the number of arguments of the method.