Found 6710 Articles for Javascript

How many values does javascript have for nothing?

Abdul Rawoof
Updated on 26-Aug-2022 11:58:57

699 Views

JavaScript has two values for nothing i.e., null and undefined. These two are also the primitive types in JavaScript. Undefined Out of the two values, Undefined in JavaScript means if a variable is declared and no value is assigned to the variable, then this variable is said to be undefined. An object can also be null. It is said to be null when there is no value to it. Example 1 This example of the undefined value in JavaScript. var str console.log('The value of given variable is:', str) In the above example 1, a variable named ‘str’ is declared. ... Read More

Name some of the string methods in javascript?

Ayush Gupta
Updated on 19-Sep-2019 07:37:38

291 Views

The String object lets you work with a series of characters; it wraps Javascript's string primitive data type with a number of helper methods. As JavaScript automatically converts between string primitives and String objects, you can call any of the helper methods of the String object on a string primitive.Following are some of the methods available for strings in JavaScript −concat() −Combines the text of two strings and returns a new string.indexOf() −Returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found.lastIndexOf() −Returns the index within the calling String ... Read More

Write the usage of split() method in javascript?

Ayush Gupta
Updated on 19-Sep-2019 07:33:18

248 Views

The split([separator, [limit]]) method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.Example usage of split methodlet a = "hello, hi, bonjour, namaste"; let greetings = a.split(', '); console.log(greetings)Output[ 'hello', 'hi', 'bonjour', 'namaste' ]Note that the commas were removed here. Any separator provided will be removed.If separator is an empty string, str is converted to an array having one element for each character of str.Examplelet a = "hello"; console.log(a.split(""))Output[ 'h', 'e', 'l', 'l', 'o' ]If no separator is provided, the string is ... Read More

How to Create GUID / UUID in JavaScript?

Arnab Chakraborty
Updated on 04-Apr-2023 11:21:05

12K+ Views

The Globally Unique Identifier (GUID) or (Universally Unique Identifier) is a 16-byte or 128- bit binary value that is used as an identifier standard for software construction. This 128-bit number is represented in a human-readable format by using the canonical format of hexadecimal strings. One example is like: de305d84-75c4-431d-acc2-eb6b0e5f6014. In this article, we shall cover how we can use javascript functionality to generate GUID or UUID. There are a few different methods, covered one by one: By using random number generation Example function generate_uuidv4() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { ... Read More

How to convert a string to camel case in JavaScript?

Ayush Gupta
Updated on 19-Sep-2019 07:15:02

1K+ Views

Camel case is the practice of writing phrases such that each word or abbreviation in the middle of the phrase begins with a capital letter, with no intervening spaces or punctuation. For example, Concurrent hash maps in camel case would be written as −ConcurrentHashMapsWe can implement a method to accept a string in JavaScript to convert it to camel case in the following way −Examplefunction camelize(str) {    // Split the string at all space characters    return str.split(' ')       // get rid of any extra spaces using trim       .map(a => a.trim())     ... Read More

Is there is a standard function to check for null, undefined, or blank variables in JavaScript?

Abdul Rawoof
Updated on 26-Aug-2022 11:54:34

587 Views

No there is not a standard function to check for null, undefined or blank values in JavaScript. However, there is the concept of truthy and falsy values in JavaScript. Values that coerce to true in conditional statements are called truth values. Those that resolve to false are called falsy. According to ES specification, the following values will evaluate to false in a conditional context − null undefined NaN empty string ("") 0 false This means that none of the following if statements will get executed − if (null) if (undefined) if ... Read More

Why is using “for…in” with array iteration a bad idea in javascript?

Ayush Gupta
Updated on 19-Sep-2019 06:57:52

154 Views

Using for..in loops in JavaScript with array iteration is a bad idea because of the following behavior −Using normal iteration loops −Examplelet arr = [] arr[4] = 5 for (let i = 0; i < arr.length; i ++) {    console.log(arr[i]) }Outputundefined undefined undefined undefined 5If we had iterated over this array using the for in construct, we'd have gotten −Examplelet arr = [] arr[4] = 5 for (let i in arr) {    console.log(arr[i]) }Output5Note that the length of the array is 5, but this still iterates over only one value in the array.This happens because the purpose of ... Read More

What is the syntax to define enums in javascript?

Arnab Chakraborty
Updated on 23-Aug-2022 08:09:44

15K+ Views

Enums or Enumerated types are special data types that set variables as a set of predefined constants. In other languages enumerated data types are provided to use in this application. Javascript does not have enum types directly in it, but we can implement similar types like enums through javascript. In this article, we shall cover the syntaxes and uses to define enumerated types in javascript. Below the syntax is given to show a basic implementation of enums in javascript, we can define an object to encapsulate the enumerated type, and assign keys for each enum value. Syntax const EnumType ... Read More

What is javascript version of sleep()?

Arnab Chakraborty
Updated on 23-Aug-2022 07:59:27

794 Views

Sometimes we perform certain tasks in any language by maintaining a time delay. For instance, we are making a timer application that will update each second. In such cases, we wait for a second and update the second timer one by one. We also call this delay or sleep().In some other languages like Java, there is a sleep() function that is used to wait for some time. In this article, we shall see what is the equivalent method of sleep in javascript. Let us follow the syntaxes for delay generation Syntax (Defining a function) function sleep(t: the time in ... Read More

What is the most efficient way to deep clone an object in JavaScript?

Abdul Rawoof
Updated on 26-Aug-2022 11:57:23

1K+ Views

In JavaScript, objects are the collection of a key value pairs. The properties of the object are the keys and is denoted with a string. The value of the key is the value of the property of the given object. In JavaScript, the objects can be copied to other by many ways in which some of them are discussed below. Using spread operator(…), Using assign() function and Using JSON.parse() and JSON.stringify() functions. Using the Json.parse() and Stingify() methods Among the above mentioned three ways, for an object to be deep cloned, JSON.stringify() and JSON.parse() functions are used. ... Read More

Advertisements