Javascript is one of the most trending and highly paid language in the tech industry right now.In my early days of my career, I struggled with javascript and soon realized that knowledge of implementing a slider or a plugin is not enough and I have to improve my basics to play with javascript. This article nucleus is “Practical Introduction to Javascript“.
Let’s begin with the very basic.
Window vs Document
Window: In very simple words “every tab is a window”. For example, window.close works when we close the tab.
Document: Whatever user see inside the tab is a document. For example, document.ready works when the page is fully loaded.
Alert vs Console.log
One of the worst and irritating way of checking output and debugging is “alert”. Javascript has got “console.log” function which is used to output data in the console of the browser.It has following benefits:
- doesn’t freeze web page, it is non-blocking
- shows very large output easily
- easy to understand and explore insight of javascript objects
Validation
Generally, first thought comes in a fresher mind after hearing validation is form validation but here I explain you about “Variable Validation“. A variable can store form value, a random string, an array, an object or any value. Consider a scenario where we have to validate a variable and confirm that it contains some string value then the process I prefer is follows:
- check whether the variable value is undefined or not
- check whether the variable value is null or not
- check for string length
Below function will solve the above problem with ease.
1 2 3 4 5 6 7 8 |
function checkVariableValue(value) { value = value.trim(); if( typeof value === "undefined" || value == null || value.length == 0 ) { return false; } else { return true; } } |
Array vs Object
When i take interviews, 95% of freshers don’t know the difference between javascript arrays and objects and believe me these two are the most used data type in javascript while developing fronted of any application.
Arrays can be “single dimensional only” in javascript. Eg.
1 2 3 4 |
var technologies_array = [] // initializing array var technologies_object = {} // initializing object var technologies = ["js","php","java",".net"]; //initializing array with values var tech_salaries = {"js":"100k","php":"90k","java":"80k",".net":"70k"}; //initializing object with values |
Looping
1 2 3 4 5 6 7 8 |
// Looping on arrays for ( key in technologies ) { console.log("Value at "+key+" key is "+technologies[key]) } //Lopping on objects for ( property in tech_salaries ) { console.log("Salary of "+property+" developer is "+tech_salaries[property]) } |
Adding values to array and object
1 2 3 4 5 6 |
// adding value to javascript array technolgies.push("html"); console.log(technologies) // adding value to javascript object tech_salaries.html = "60k"; console.log(tech_salaries) |
Above I explained some of the practical tips of javascript which I thought every newbie must learn to grow and hope these will be helpful for every developer thinking of learning javascript. Got any question, ask me now.