|  | 

Programming

JavaScript Commands Every Programmer Should Know

img-responsive
(adsbygoogle = window.adsbygoogle || []).push({});

Every mobile app and web application use some JavaScript for more dynamic and engaging user interactions. Developers love it because it is easy to learn with only has a few rules. You can do a lot with only a few lines of code.  However, it lacks structure, which can lead to unexpected, difficult-to-find bugs.  

You might want a JavaScript cheat sheet to overcome this issue. That way, you will have a nice list of useful commands you can reference as you need them. Here are a few of the more popular commands to get you started.  

document.getElementById() 

JavaScript lets you execute code based on a particular HTML name or class, but it can slow down your code. Instead, you use this command and your applications document object model (DOM) to speed things up can speed up.  

Basic Input / Output 

In JavaScript, this user interaction requires the alert() and prompt() commands. Alert sends info to the user through a dialog box, while prompt asks for a response.  

alert("message"); 
stringVar = prompt("message") 

SetTimeout() 

SetTimeout automatically runs your commands without user input. It just requires the command you want to run and when you want to run it in milliseconds.  

setTimeout(command, time); 

Setinterval() 

You can repeatedly run commands using the setinterval command.  

setInterval(command, time); 

ClearTimeout() and ClearInterval() 

If you will use either settimeout or setinterval, you want their associated clear command as well. ClearTimeout and ClearInterval deletes set command variables from memory.  

$timer = setInterval(command, 5000); 
 
clearInterval($timer); 

Var 

Var formally creates your variables. Using this command prevents you from accidently overriding your  variables while speeding up your code.  

Self-calling Function 

Self-calling functions run a is a JavaScript command that runs as soon as you create it. These commands are also called Self-Invoked Anonymous Functions or Immediately Invoked Function Expressions (IIFE). 

(function(){    // some private code that will be executed automatically}) 

Math.random() 

The command generates random numbers. 

Map() 

The map() command lets you loop through an array or collection.  

Debounce() 

Debounce limits how often a command will run.  

Poll() 

This command lets you see if something stops or if there is an exception.  

Once() 

Once lets you run a command once and only once during the execution of an application.  

GetAbsoluteUrl() 

This command gets you the actual url of a web address, especially if it is contained in a string. It just takes the url string and it gives back the address with all the query and other elements removed.  

var getAbsoluteUrl = (function() {var a;return function(url) {if(!a) a = document.createElement('a');a.href = url;return a.href;};})();// UsagegetAbsoluteUrl('/something'); //  

IsNative()  

This function indicates if a given browser feature is native to a particular browser or if it comes from a third-party app.  

String.substr() 

Substr dives into a string and returns text based on the provided criteria. You must specify where you want it to start, and how many characters you want the command to retrieve.  

Some of these commands come with the language. Others are useful snippets of code you can add when needed. Either way, these command swill help you improve your JavaScript.

This article is a recreating of an article I previously did for a client.
(adsbygoogle = window.adsbygoogle || []).push({});
javascript-commands-every-programmer-should-know

ABOUT THE AUTHOR

POST YOUR COMMENTS

Your email address will not be published. Required fields are marked *

Name *

Email *

Website

Skip to toolbar