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

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

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

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

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

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 2622

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T07:07:07+00:00 2024-11-26T07:07:07+00:00

JS being odd. Some weird things about javascript.

  • 61k

Many of us will agree that Javascript is one of the best languages around, but sometimes some things can make us some headaches.

Here is a list of five weird things Javascript has. Please feel free to add your favorites in the comments.

null acting weird.

As the documentation said, The value null represents the intentional absence of any object value. But if I tell you null is an object? Just check by yourself.

console.log(typeof null) // log: object 
Enter fullscreen mode Exit fullscreen mode

Weird, right?. But don't worry, it doesn't look so weird when we check that null is not an instance of an object, making its definition valid.

console.log(null instanceof Object) // log: false 
Enter fullscreen mode Exit fullscreen mode

Side note: [] also is an object but, in that case, is an instance of an object too.

Number literals as objects

Except for null and undefined, everything in Javascript acts like an object, and number literals are not the exception.

So, try this:

console.log(10.toString()) 
Enter fullscreen mode Exit fullscreen mode

Is failing, right? You probably will see an error similar to this: error: unknown: Identifier directly after number (1:15).
This is happening because javascript's parser will read the . notation as a floating-point and fail. To avoid this, here are some workarounds.

console.log(10..toString())  console.log((10).toString()) 
Enter fullscreen mode Exit fullscreen mode

NaN is a number.

Yes, I know, the literal definition of NaN is “Not a Number,” but:

console.log(typeof NaN); // Will return number 
Enter fullscreen mode Exit fullscreen mode

and not only that

console.log(NaN == NaN) // Will return false. 
Enter fullscreen mode Exit fullscreen mode

This could be because NaN is not equal to anything, being isNaN() the only way to check for it.

Be careful with parseInt()

parseInt will take any string and parse it to an integer of the specified base we pass as the second parameter.

console.log(parseInt("10", 10)); // will return 10   console.log(parseInt("flowers")); // will return NaN 
Enter fullscreen mode Exit fullscreen mode

But look if we change the base in the last statement:

console.log(parseInt("flowers", 16)); // will return 15 
Enter fullscreen mode Exit fullscreen mode

Even the word “flowers” is not a valid hex parseInt() will evaluate character by character instead of the entire word, so in this case, is returning the value for the f.

Check your return

Consider the following code:

function example() {   return   {     value: "string"   } }  console.log(example()); // Logs: undefined 
Enter fullscreen mode Exit fullscreen mode

Just by being minimally familiar with javascript, you will realize something strange in that new line after the return statement.
What in many languages can be a style issue in JS has another effect. JS, behind curtains, inserts a semicolon after most newlines causing in the example that we never reach to return the object. So if we want to make it right:

function example() {   return {     value: "string"   } }  console.log(example()); // Logs: {value:"string"} 
Enter fullscreen mode Exit fullscreen mode

Finishing up

These were only a few of tens of weird things Javascript has to offer. I promise I will add some other later.
Please comment on which other do you know and if they gave you a hard time.

Thanks for reading!

beginnerscodenewbiejavascriptwebdev
  • 0 0 Answers
  • 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

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

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

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

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.