• Home
  • PHP
  • MySQL
  • Laravel
  • Demos
  • HTML
  • jQuery
  • Framework
  • Request Tutorial
PHP Lift
  • Home
  • Demos
  • Advertisement
PHP Lift
  • Home
  • PHP
  • MySQL
  • Laravel
  • Demos
  • HTML
  • jQuery
  • Framework
  • Request Tutorial
  • Follow
    • Facebook
    • Twitter
    • Google+
    • Pinterest
    • Youtube
    • Instagram
    • RSS
JavaScript tips and tricks
Home
JavaScript

JavaScript tips and tricks

April 30th, 2025 Ivan Dimov JavaScript, Tips 0 comments

  1. Global scope

Use objects/classes to encapsulate your application logic and try not to pollute the global variable scope as this can cause frustrating problems such as libraries not working from variables being overridden.

Below is an example how you can structure your application’s logic in a simple way without polluting the global scope.

var app = {};



app.initialize = function() {



//do sth



}

app.someAction = function() {



// do sth else



}

app.properties = {}

app.properties.isStarted = 1;
  1. Chain methods wherever possible

Method chaining in JavaScript can simplify your application’s logic. To chain methods, the previous method must return the instance that it operates on,  and the next method will make changes to that instance and again return it. jQuery is a good example of the possibility for method chaining:

$(“div”).css(“background-color”, “crimson”)



.height(400)



.css(“border”, “4px solid green”)

In the example above, we work with the selected divs to perform as many changes on them as we want.

  1. Use the strict equals comparison

The equals comparison (==) would hold a truthy value even if the variables are not of the same type which often cause problems. For example, “” == 0 would return True, which may not be what you want if you at a point want to check if the first operand is zero and not just a falsy value. Therefore, it is best to stick to using the strict equals operator (===) , in most situations, which checks both the value and the type. With the strict equals, “” === 0 would return False and only 0 === 0 would return True.

  1. Short-circuit conditionals

If you can arrange your code to execute a function if a particular condition is met, you can short-circuit that in a single line.

In other words, instead of:

If (sth) {



doSthElse();



}

You can just write sth && doSthElse();.

  1. Setting default variable values

Somewhere in your application, you might be setting up your variables like this:

If (!alpha) {



var alpha = 42;

}

You can set default values in a one-liner using the OR operator, like this:

var alpha =  alpha || 42;
  1. Getting array elements in reverse order

If you want to reverse an array, you can use the reverse method on the array, like this:

var arr = [1,3,5,6,7];



arr.reverse()



[7, 6, 5, 3, 1]

However, if you just want to get a specific number of elements off the back of the array, you can use the slice method with a negative first parameter, like this:

arr.slice(-3)



[5, 3, 1]



You just have to replace -3 with the number of array items from the back of the array that you want.

  1. Use strict mode

By starting your scripts with the string “use strict”; you save yourself from some of the common code mistakes that could be made, such as declaring global variables within a local scope (skipping the var keyword).

For example,

myVariable = 3.14;       // This will not cause an error unless use strict is found in that scope or the global scope.

Please subscribe for more tricks.

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook

Related

Facebook Twitter Google+ LinkedIn Pinterest
Next article Web 3.0 and PHP: A Comprehensive Guide
Previous article Design Patterns in Software Development

Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter

Related Posts

How to create a screen recorder in JavaScript JavaScript
June 16th, 2025

How to create a screen recorder in JavaScript

9 Tricks and Tips to Search Google Like a Pro Tips
June 16th, 2025

9 Tricks and Tips to Search Google Like a Pro

Top Laravel packages for advanced programmers Framework
June 16th, 2025

Top Laravel packages for advanced programmers

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe
Get new posts by email:
Powered by follow.it
Advertisement
Like us
Recent Posts
  • 8 interesting functions of Laravel Eloquent (ORM)
  • How to create a screen recorder in JavaScript
  • Top Programming Languages in 2024
  • 9 Tricks and Tips to Search Google Like a Pro
  • Top Laravel packages for advanced programmers
Categories
  • API
  • Bootstrap
  • Bot
  • CSS
  • CSS 3
  • Database
  • Designing
  • Framework
  • Guide
  • HTML
  • HTML 5
  • JavaScript
  • jQuery
  • Laravel
  • MySQL
  • Node.js
  • oAuth
  • Payment
  • PHP
  • Python
  • Social
  • Tips
  • Web 3.0
  • WordPress
Weekly Tags
  • PHP
  • How to
  • javascript
  • laravel
  • MYSQL
  • PHP framework
  • css
  • jQuery
  • HTML to PDF
  • Web Development
  • About
  • Privacy Policy
  • Back to top
© PHPLift.net. All rights reserved.