Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7549521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:57:52+00:00 2026-05-30T09:57:52+00:00

I was reading John Resig’s Secrets of JavaScript Ninja and saw this code: function

  • 0

I was reading John Resig’s Secrets of JavaScript Ninja and saw this code:

 function Ninja(){
   this.swung = false;

   // Should return true
   this.swingSword = function(){
     return !!this.swung;
   };
 }

I know !! is used to convert an expression into boolean. But my question is why does he use:

return !!this.swung;

Isn’t that redundant because swung is already a boolean variable or am I missing something ?

BTW here is full relevant code just in case:

 function Ninja(){
   this.swung = false;

   // Should return true
   this.swingSword = function(){
     return !!this.swung;
   };
 }

 // Should return false, but will be overridden
 Ninja.prototype.swingSword = function(){
   return this.swung;
 };

 var ninja = new Ninja();
 assert( ninja.swingSword(), "Calling the instance method, not the prototype method."
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:57:54+00:00Added an answer on May 30, 2026 at 9:57 am

    this.swung not a local variable, but a property of Ninja‘s instances. So, the property can be modified by an external method.

    To make sure that swingSword always return a boolean, an explicit conversion using !! is useful.

    As for your code: I believe that it should be !this.swung, because !!this.swung returns false for this.swung = false:

    this.swung = false;                                          // Defined in code
    !!this.swung === !!false;                                    // See previous line
                     !!false === !true;                          // Boolean logic
                                 !true === false;                // Boolean logic
                                           false === this.swung; // See first line
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I was reading up on John Resig's blog , saw his micro-templating javascript
I m reading John Resig s Learning Advanced JavaScript http://ejohn.org/apps/learn/#10 and came across this
I am a newbie to java script and currently reading John Resig's Pro javascript
I'm reading the John Resig blog about the Strict Mode in javascript, but i
Reading this question I found this as (note the quotation marks) code to solve
Reading through this question on multi-threaded javascript, I was wondering if there would be
Reading over the responses to this question Disadvantages of Test Driven Development? I got
Reading source code of sample projects, such as Beast and Bort, are recommended as
Reading this post has left me wondering; are nightly builds ever better for a
I'm attempting to learn some flex/bison, and I'm reading Flex & Bison by John

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.