Getting console error num.html:8 Uncaught ReferenceError: Invalid left-hand side expression in postfix operation at num.html:8 (anonymous) @ num.html:8


#1
<html>

<script>
    function intValue() {
    return 5;
    }

    console.log("hello " + (intValue()++));
</script>
</html>

#2

what is the purpose of ++?


#3

To increment a value.


#4

a few things are going wrong here.

intValue()++ is not going to work. What you are saying here is increment the function intvalue() by 1. This is not possible because a function is not an integer. This is also why the browser gives you the error.
A fix for this is to first put the returned value inside a variable.

temp = intvalue();
console.log("hello " + temp++);

Now this resolved the error but it is still not incrementing the value. This is because ++ is used when you are looping over something. Since it is only being called once, it is not going to increment.

In this example ++ is used inside a loop and than it will start working. Note how the first call it does not increment.

<script>
    function intValue() {
		return 6;
    }
	function count(){
		temp = intValue();
		for(i = 0; i < 10; i++){
			console.log("hello " + temp++);
		}
	}
	count();
</script>