++Somevariable Vs. Somevariable++ in JavaScript

++someVariable vs. someVariable++ in JavaScript

Same as in other languages:

  • ++x (pre-increment) means "increment the variable; the value of the expression is the final value"
  • x++ (post-increment) means "remember the original value, then increment the variable; the value of the expression is the original value"

Now when used as a standalone statement, they mean the same thing:

x++;
++x;

The difference comes when you use the value of the expression elsewhere. For example:

x = 0;
y = array[x++]; // This will get array[0]

x = 0;
y = array[++x]; // This will get array[1]

What is this.somevariable means in javascript?

this is a keyword in JavaScript.

function test(){
  this.x = 10;
}

in this case this represents the internal object, only can use in the function.

With the function of different occasions, this's value will change. But there is a general principle: this means the object which invoke the function.

Case 1:
pure function call:

This is the most common use of the function, belongs to the global call.
So this just means of the global object Global.

e.g:

function test(){
    this.m = 10;
    alert(this.m);
  }
  test(); // there alert 10

In order to prove this is a global object:

   var m = 10;
  function test(){
    alert(this.m);
  }
  test(); // 10

And:

   var m = 10;
  function test(){
    this.m = 0;
  }
  test();
  alert(m); // 10

Case 2:
As the object which calls the function.

 

function test(){
    alert(this.m);
  }
  var obj = {};
  obj.m = 10;
  obj.n = test;
  obj.n(); // 10

Case 3:

As a constructor

function test(){
    this.m = 10;
  }
  var obj = new test();
  alert(obj.m); // 10

In order to show that this is not a global object in this case:

   var m = 10;
  function test(){
    this.m = 1;
  }
  var o = new test();
  alert(m); //10

What is the difference between !!variable and variable

The double not operator !! coerces a (potentially non-boolean) value to a boolean.

In your specific example:

var someVariable = (someOtherVariable === 'true');
if (!!someVariable) {
// do some stuff here
}

someVariable is already guaranteed to be a Boolean (since the result of an === comparison is always a Boolean) so coercing it to a Boolean does not change the operation in any way and is pretty much wasted code. Even if it wasn't already a Boolean, you don't need to coerce it to a Boolean just to test it like if (someVariable) either so there's yet another reason not to use the !! here.

When !! is useful is when you want to store a true Boolean somewhere, but you may only have a truthy or falsey value, not necessarily a true Boolean. Then you can coerce it to a Boolean with the !!.


So, suppose you had some value that is not necessarily a Boolean and you wanted to set some other value to a true Boolean based on the truthy-ness or falsey-ness of the first variable. You could do this:

var myVar;
if (someVar) {
myVar = true;
} else {
myVar = false;
}

or this:

myVar = someVar ? true : false; 

or this:

myVar = !!someVar;

why variable++ is not working the same way as variable+1

let x = 3
let y = x++
//output: x=4 y=3

let x = 3
let y = ++x
//output: x=4 y=4

It's why you need to double click because you have next logic:

console.log(counter) //0
//triggers click
console.log(counter) //0, useState re-assigned to 0
//triggers click
console.log(counter) //1, useState re-assigned to 1

(null != someVariable) OR (someVariable != null)

They're equivalent.

However, the first one will cause an invalid assignment error if you mistype != as =. Some people like this as it's rather easy to type = instead of ==, although the former isn't always an accident.

You can see the precise rules of the specification with regard to the == operator on the Annotated ES5.

What's the difference between ++i and i++ in JavaScript

++i returns the value of i after it has been incremented. i++ returns the value of i before incrementing.

When the ++ comes before its operand it is called the "pre-increment" operator, and when it comes after it is called the "post-increment" operator.

This distinction is only important if you do something with the result.

var i = 0, j = 0;

alert(++i); // alerts 1
alert(j++); // alerts 0

One thing to note though is that even though i++ returns the value before incrementing, it still returns the value after it has been converted to a number.

So

var s = "1";
alert(typeof s++); // alerts "number"
alert(s); // alerts 2, not "11" as if by ("1" + 1)

Is it normal to use in the function some variable with the same name as it function?

It's generally bad practice to "shadow" variable names. It can cause confusion about what's being referenced if you aren't careful.

In this example, there isn't a major downside. Consider though if later you decided to make the function recursive. If you tried to call sum from within itself, you'd get an error that sum isn't a function, because it's finding the inner variable sum, not the function. That's not a major issue, but it's a good idea to write code that is less likely to break in weird ways in the future. You never know what changes you might make later on.



Related Topics



Leave a reply



Submit