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

  • Home
  • SEARCH
  • 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 7863745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:34:50+00:00 2026-06-02T23:34:50+00:00

Javascript Code var a = {}; a.test += 1; //NaN ++a.test; //NaN Instead var

  • 0

Javascript Code

var a = {};
a.test += 1; //NaN
++a.test; //NaN

Instead

var a = {};
a.test = 0;
++a.test; //1
a.test += 1; //2

I wonder if there could be anyway that can make first code sample work the same as second, i.e without an explicit assignment to 0. As in assigning default value for any property of an object to 0 instead undefined. I’m trying to do this in node.js. So, no problem of cross browser things and old ECMA Specs.

var i;
for(i = 0; i<10; i++) {
   if(a.test) {
     ++a.test;
   } else {
     a.test = 0;
     ++a.test;
   }
   //a.test = a.test || 0; (1)
   //++a.test;
}

If it is possible then the inner if/else or the assignment statement(1) in the above code can be eliminated.

  • 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-06-02T23:34:51+00:00Added an answer on June 2, 2026 at 11:34 pm

    There’s no way to do this for any arbitrary undefined property.

    For a known property name, there is a way, but DO NOT USE IT !! 1

    Object.prototype.test = 0;
    

    This will give every object an implicit .test property with that value in it, and the first time you attempt to modify it the result will be stored in your own object:

    > Object.prototype.test = 0
    > a = {}
    > a.test++
    > a.test
    1
    

    1 Adding stuff to Object.prototype will break stuff, including for (key in obj)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JavaScript array that can be built using this code var Test
Sample code: <!DOCTYPE html> <html> <head> <title>test</title> <script language=javascript type=text/javascript> function init() { var
In Javascript I can send XML string to JSP server (XmlAction.jsp): Javascript Code: var
How to write this JavaScript code without eval? var typeOfString = eval(typeof + that.modules[modName].varName);
In JavaScript let's say we have the following code var test = 'd'; if
here is javascript code: var test = { h : function (a) {return a;},
Json String: (response) {19:{id:19,name:Product,sku:123,price:59.50,cost:25.00},20:{id:20,name:Test,sku:456,price:50.00,cost:40.00}} JavaScript Code: var json = $.parseJSON(response); var items = new
I have the following javascript code. var Test = function(){ $(#+elementId).click(function(){ // How to
I'm getting an unterminated string literal Javascript error with this code: var test =
I have some troubles with this small JavaScript code: var text=Z Test Yeah !

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.