Home > JavaScript

JavaScript

Invalid JSON if integer value starts with 0

javascript

If any integer value in the JSON starts with 0, the parser will throw an error as “invalid key:value pair” or “SyntaxError: Unexpected number in JSON at position xx” . The reason of this error is because if the integer begins with leading 0 then it will consider this value as Octal number. As Octal number always starts with 0 ... Read More »

Popup-under hack

javascript

Popup-under hack If you are working on a mobile site, you have a constraint of screen size. What if you are loading a URL from a page using window.open(targetURL,’_blank’);. For your information browser will only focus on the window which is currently opened. So, in this case, your window loading targetURL will get focus. And, suppose your boss says to ... Read More »

Javascript Constructors- Why it should be used- Object Oriented Programming in Javascript

javascript

Javascript Constructors- Why it should be used- Object Oriented Programming in Javascript Mark my words! Javascript is an object oriented programming language, i.e you can create objects to solve your programming problems. In object oriented approach, you create objects that represent things in the system and you add logic for objects to behave, and then objects interact with one another ... Read More »

Closures: A concept of callback function – Functional programming in Javascript Part 4

javascript

Closures: A concept of callback function – Functional programming in Javascript Part 4 Closures in Javascript are perceived to be fairly complicated and advanced, this discourages people to learn Javascript. I will try to make it simple. To be frank, closures are now used in many languages, people use them in day-to-day coding, whether they know it or not. Because ... Read More »

Avoid polluting global namespace in Javascript- IIFE (Immediately Invoked Function Expression)

javascript

Avoid polluting global namespace in Javascript- IIFE (Immediately Invoked Function Expression) Everybody says Global variables are bad, they are evil, it produces abnormal behavior. In the browser environment everything you load is in a single space. An HTML page may be linked to 1, 2, or 10 JS files. Each JS file may contain code that is written by completely ... Read More »

Understanding scopes in Javascript- Function Scoping

javascript

Understanding scopes in Javascript- Function Scoping The concept of scopes are in every language. A scope dictates a portion of program where a particular variable is accessible. Not all variables are accessible in every places of program. if variables are allowed access everywhere then there will be chaos. There are controlled portions in program where variables are active. In C++ ... Read More »

JavaScript function declaration syntax: var fn = function() {} vs function fn() {}

javascript

JavaScript function declaration syntax: var fn = function() {} vs function fn() {} In Javascript there are 2 ways to create functions:- function declaration:- function fn(){ console.log("Hello"); } fn(); This is very basic, self-explanatory, used in many languages and standard across C family of languages. We declared a function defined it and executed it by calling it. What you should ... Read More »

forEach method in Arrays – Functional Programming in Javascript Part 3

javascript

forEach method in Arrays – Functional Programming in Javascript Part 3 In Javascript arrays are objects. It has a method forEach(fn) that takes function as argument and executes it for each element of the array. This function can have element of the array, index and array itself as argument. var myArray=[10, 20, "Hello", {}]; var myFunction=function(item, index, array){ console.log("For an ... Read More »