Do You Have Trust Issues?

While having trust issues is not considered a positive thing in the “normal” world, if you work in the programming field, this is a necessity. Why? Most programs interact with end users in some shape or fashion. When we allow end users to play apart in our program, there is always a chance of something going wrong. Here are some possibilities…

  • Asking the end user for a number and they put in text
  • Needing certain pieces of information and the end user does not provide it

Plus many other scenarios. Fortunately, programming languages provide us with ways to protect our programs and provide a nice experience for end users.

There are many ways to ensure that the end user provides you with the necessary information for your program to run. For example, in HTML5, there is a required attribute. In JavaScript, you can also test to see if a field is empty with an ‘IF’ statement.

[code lang=”js”]
// Create empty variable
var emptyVar = "";
// Test to see if the variable is empty
if(emptyVar === ""){
// Do something
}
[/code]

Another example is working with integers and strings. We want to avoid concatenation errors. So that is where numeric functions come into play. We also have the ability to test whether data is a number or not. We can do that with the isNaN() method.

The isNaN() method is an interesting method because it may not be as intuitive as one thinks unless they break down what the function is asking. Right off the bat when looking at a number such as 123, one would want to know if that is truly an integer or not and expect to receive a true response. When run through the isNaN() funtion, the response is false. Let’s break down what the function is asking…isNaN()…basically, it is asking, “Is the information pass to me not a number?” In other words, as in our example, “Is ‘123’ not a number?” Well, ‘123’ is a number. So the answer is false. Another way of looking at this function is if you want integers, look for false responses. Otherwise, it is text.

[code lang=”js”]
// Create variables to house data to test
var testVar1 = isNaN(123);
var testVar2 = isNaN(-1.23);
var testVar3 = isNaN(5-2);
var testVar4 = isNaN(0);
var testVar5 = isNaN("Hello");
var testVar6 = isNaN("2014/15/09");
// The aforementioned would result in the following boolean output
false
false
false
false
true
true
[/code]

I know for some that isNaN() can be somewhat confusing. I hope the code examples clarifies things for you.

Happy Coding!

Clay Hess

More To Explore

Code

Exploring the Broadcast Channel API: Inter-Tab Communication

Intercommunication between different contexts (like tabs, iframes or workers) of the same origin has often been a challenge. With the Broadcast Channel API, developers now have a powerful tool to easily communicate between browsing contexts. In this blog post, we’ll dive deep into the capabilities of the Broadcast Channel API, exploring its features, use cases, and how it can be effectively implemented in your projects.

computer, laptop, work place-2982270.jpg
Code

Unlocking Wireless Communication: A Dive into the Bluetooth API

Wireless communication has become an integral part of our daily lives, and Bluetooth technology is at the forefront of this revolution, enabling devices to exchange data over short distances and creating a world more interconnected than ever before. At the heart of this technology lies the Bluetooth Application Programming Interface (API), a powerful tool for developers looking to harness the capabilities of Bluetooth in their applications. In this blog post, we’ll explore what the Bluetooth API is, how it works, and the possibilities it opens up for innovation in wireless communication.

Share This Post

Need help?

Let's have a chat...


Login

Jump Back In!

Here at Webolution Designs, we love to learn. This includes sharing things we have learned with you. 

Register

Begin Your Learning Journey Today!

Come back inside to continue your learning journey.