Open In App

Lodash _.castArray() Method

Last Updated : 03 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Lodash _.castArray() method is used to cast value into an array if it is not an array.

Syntax: 

_.castArray(value);

Parameters:  

  • value: This parameter holds the value that needs to be inspected.

Return Value:

  • It returns an array with the including value passed in the _.castArray().

Example 1: In this example, we are using an integer value as a parameter. 

JavaScript
const _ = require('lodash');
let x = 10;
let arr = _.castArray(x);
console.log("\n\nThe value returned to by _castArray(x) is", arr);

Output: 

Example 2: In this example, we are using String, null, and undefined as a parameter.  

JavaScript
const _ = require('lodash');

let x = _.castArray('abc');
console.log('\n With String ', x);

let y = _.castArray(null);
console.log('\n With null ', y);

let z = _.castArray(undefined);
console.log('\n With undefined ', z);

Output: 

Example 3: In this example, we are using the _.castArray() method with no parameter, object, and function. 

JavaScript
const _ = require('lodash');

let x = _.castArray();
console.log("\n With no parameter ", x);

let y = _.castArray({
    "name": "lodash",
    "work": "I'm make js more"
});
console.log("\n With object ", y);

let z = _.castArray(function hello() {
    console.log("hello");
});
console.log("\n with function ", z);

Output: 

Example 4: In this example, we are using multiple parameters, it will take only the first parameter and with an array, it will just return the same array. 

JavaScript
const _ = require('lodash');

let x = _.castArray(1, 2, "hello");
console.log('\n With multiple parameter ', x);

let y = _.castArray([1, 2, 3]);
console.log('\n With array ', y);

Output: 




Next Article

Similar Reads