最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What does var = var && "*" mean in Javascript - Stack Overflow

programmeradmin4浏览0评论

I am trying to work out some obfusicated code by reading it, and I was doing pretty well until a came across this:

a = a && "*"

Now I am still quite new to Javascript and these shortened unmon javascript codes are still very foreign to me, this is the first time I have e across them.

Does anybody know what this does? I attempted it in a javascript code tester ad it just returned *, so I do not know.

Also, if anybody knows where I can look to find out what these unmon lines of code do, that would be very helpful. They are all shortened and are sorts of things like this and

a = a || b

(I know what that one does)

But If there is some sort of name for this kind of javascript or a reference I can look at, that would be very helpful, I have been scouring Google for hours.

Thanks

I am trying to work out some obfusicated code by reading it, and I was doing pretty well until a came across this:

a = a && "*"

Now I am still quite new to Javascript and these shortened unmon javascript codes are still very foreign to me, this is the first time I have e across them.

Does anybody know what this does? I attempted it in a javascript code tester ad it just returned *, so I do not know.

Also, if anybody knows where I can look to find out what these unmon lines of code do, that would be very helpful. They are all shortened and are sorts of things like this and

a = a || b

(I know what that one does)

But If there is some sort of name for this kind of javascript or a reference I can look at, that would be very helpful, I have been scouring Google for hours.

Thanks

Share Improve this question edited Jan 19, 2019 at 2:02 connexo 56.9k15 gold badges111 silver badges145 bronze badges asked Dec 24, 2012 at 9:56 archarch 4701 gold badge9 silver badges19 bronze badges 3
  • Duplicate of What does “options = options || {}” mean in Javascript? and JavaScript. What does this expression mean: “ var a = b === c && d; ”. It's called short-circuit evaluation, read more at developer.mozilla/en-US/docs/JavaScript/Reference/Operators/… – Rob W Commented Dec 24, 2012 at 10:00
  • Could have been written as (i) if (a) {a="*";} (ii) if (!a) {a=b;} – Salman Arshad Commented Dec 24, 2012 at 10:10
  • @SalmanA Yes, that's correct. – Rob W Commented Dec 24, 2012 at 10:21
Add a ment  | 

1 Answer 1

Reset to default 8

If a is truthy, it assigns "*" to a.

If a is falsy, it remains untouched.


&& has short-circuit semantics: A pound expression (e1) && (e2)—where e1 and e2 are arbitrary expressions themselves—evaluates to either

  • e1 if e1 evaluates to false in a boolean context—e2 is not evaluated
  • e2 if e1 evaluates to true in a boolean context

This does not imply that e1 or e2 and the entire expression (e1) && (e2) need evaluate to true or false!

In a boolean context, the following values evaluate to false as per the spec:

  • null
  • undefined
  • ±0
  • NaN
  • false
  • the empty string

All1 other values are considered true.

The above values are succinctly called "falsy" and the others "truthy".

Applied to your example: a = a && "*"

According to the aforementioned rules of short-circuit evaluation for &&, the expression evaluates to a if a is falsy, which is then in turn assigned to a, i.e. the statement simplifies to a = a.

If a is truthy, however, the expression on the right-hand side evaluates to *, which is in turn assigned to a.


As for your second question: (e1) || (e2) has similar semantics:

The entire expression evaluates to:

  • e1 if e1 is truthy
  • e2 if e1 is falsy

1 Exception

发布评论

评论列表(0)

  1. 暂无评论