0

I did C( and a bit of C++)programming all my life till now and started to learn Javascript recently.

I am having tough time to understand some of the Javascript Syntax.

Can someone explain me the following line :

window.$N = $N || {};

Thanks!

Brad
  • 159,648
  • 54
  • 349
  • 530
thipS
  • 1
  • 2
    if global variable `$N` is defined (or truthy, to be pedentic) leave it as is, if not, declare it as an empty object. – pawel Oct 30 '14 at 15:00
  • It's a default value. The expression `$N || {}` means "`$N` if `$N` is 'truthy' otherwise `{}`". – amphetamachine Oct 30 '14 at 15:00

1 Answers1

0

In browsers, the window object is global.

If $N is falsy, {} will be assigned to window.$N. If $N is truthy, its value will be assigned to window.$N (keeping the same value probably).

This is a common way to override default values.

Brad
  • 159,648
  • 54
  • 349
  • 530