JavaScript Snippets
193 snippets in JavaScript.
- Colors in JavaScript Console
The console.log() method outputs a message to the web console. Read the tutorial and learn the simplest method of putting a single or multiple styles to it.
- Form Validation Using JavaScript
Incorrect information leads to bad user interactions. So as to prevent this problem, read and learn how to validate forms with the help of JavaScript!
- How and When to Write Comments in Javascript
There are 2 types of JavaScript comments: single-line and multi-line. See when to use them with examples, and also use comments to prevent execution when testing alternative code.
- How Does the Access-Control-Allow-Origin Header Work
Read this tutorial and learn about how the Access-Control-Allow-Origin response header works. Also, read about CORS, the origin, and non-simple requests.
- How Get the Current Year in JavaScript
Read this tutorial and learn about the simplest and fastest method of getting the current year a four-digit number using the Date object in JavaScript.
- How JavaScript Closures Work
In the following snippet, we are going to represent how JavaScript closures work. The easiest way of learning how to use them is investigating examples.
- How the Keyword “this” Works
In this tutorial, you can find information about the usage of the keyword “this” in JavaScript. Here, it works differently compared with other languages.
- How to Access the Correct “this” Inside a Callback
In the following tutorial, you can find the answer to one of the most common issues in JavaScript: how to access the correct “this” inside a callback.
- How to Add a Class to a Given Element
Read the tutorial and learn the two suggested methods of creating a JavaScript function that will add a class to the <div> element and solve your problem.
- How to Add and Remove Multiple or Single Select Options using jQuery
Read this tutorial and learn how you can remove multiple options or a specific option in the select list using some interesting and useful jQuery methods.
- How to Add Days to JavaScript Date
Read the tutorial and learn a simple method of adding days to JavaScript date with two inbuilt functions used for setting and getting the day of the month.
- How To Add New Elements To A JavaScript Array
To add new elements you can use the following JavaScript functions: push() unshift(), concat() function or splice(). See examples.
- How to Add Options to a Select Element using jQuery
Read this tutorial and find useful information about the multiple methods of adding options to a select element using jQuery. Choose the best one for you.
- How to Append an Item to an Array in JavaScript
On this page, you can learn the ways of appending an item or multiple items to an array in JavaScript.
- How to Build a Hex Color Generator in JavaScript
If you want to create your own color generator using JavaScript, then you’re welcome to our tutorial. In this snippet, we are going to build a hex color generator step by step.
- How to Calculate Text Width with JavaScript
Read this JavaScript tutorial and learn about the best method of calculating the text width. Read about the advantages of the method over other methods.
- How to Capitalize the First Letter in a String in JavaScript
Read this JavaScript tutorial and learn the methods and undertake some steps which will help you make the first letter in the string uppercase easily.
- How to Center a <div> on the Screen Using jQuery
Read the tutorial and learn the jQuery method of centering the <div> element on the screen. Get a code snippet to add your script for centering the element.
- How to Change an Element’s Class with JavaScript
One of the most common issues in JavaScript is changing an element’s class. In this snippet, we are going to show you the efficient ways of achieving it.
- How to Change Selected Value of a Drop-Down List Using jQuery
In this tutorial, you will read and learn about jQuery methods that are used to change the selected value of a drop-down list via its text description.
- How to Change the Href for a Hyperlink using jQuery
Read this JavaScript tutorial and learn the easy way of changing the href attribute for a hyperlink step by step by using one of the methods of jQuery.
- How to Change the Image Source Using jQuery
Read this tutorial which will provide you with useful information about the best and easy method that can be used to change the image source using jQuery.
- How to Check a Radio Button with jQuery
Read the tutorial and learn the right way of checking a radio button with jQuery. Also, read how to handle with different versions of jQuery while checking.
- How to Check a Webpage is Loaded Inside an iframe or into the Browser Window
Read this JavaScript tutorial and find a working code for checking whether the webpage is loaded inside an iFrame or directly into the browser window.
- How to Check and Uncheck Checkbox with JavaScript and jQuery
In this tutorial, you will read and learn about several JavaScript and jQuery methods for checking and unchecking a checkbox. Choose the best one for you.
- How to Check for a Hash Value in a URL Using JavaScript
Read this tutorial and learn the fastest and simplest way of checking for a hash value in the URL using the window.location.hash method in JavaScript.
- How to Check for Empty/Undefined/Null String in JavaScript
In this tutorial, we are going to show you the ways of checking whether the JavaScript string is empty, undefined, or null. Just follow the guidelines.
- How to Check for the Existence of Nested JavaScript Object Key
In this tutorial, you will find several methods that are used to test the existence of the nested JavaScript object keys. Also, find how to avoid TypeError.
- How to Check if a Key Exists in JavaScript Object
This snippet is explaining how to check whether a key exists in a JavaScript object. Examine the solutions given above and choose the most appropriate.
- How to Check If a String Contains Another Substring in JavaScript
Read this tutorial and learn several methods used to check if a string contains another substring in JavaScript or not. Read information and see examples.