
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
JavaScript - convert array with null value to string
We have an array that contains some string values as well as some nullish values.
We are required to write a JavaScript function that takes in this array and returns a string constructed by joining values of the array and omitting nullish values.
Following is our array, with some null and undefined values −
const arr = ["Here", "is", null, "an", undefined, "example", 0, "", "of", "a", null, "sentence"];
Let’s write the code for this function −
Example
const arr = ["Here", "is", null, "an", undefined, "example", 0, "", "of", "a", null, "sentence"]; const joinArray = arr => { const sentence = arr.reduce((acc, val) => { return acc + (val || ""); }, ""); return sentence; }; console.log(joinArray(arr));
Output
Following is the output in the console −
Hereisanexampleofasentence
- Related Articles
- How to convert string type value to array type in JavaScript?
- Convert nested array to string - JavaScript
- Convert string with separator to array of objects in JavaScript
- Convert integer array to string array in JavaScript?
- remove null value from a String array in Java
- How to convert a boolean value to string value in JavaScript?
- How to convert an array into JavaScript string?
- JavaScript - Convert an array to key value pair
- Sorting an array objects by property having null value in JavaScript
- Filter away object in array with null values JavaScript
- Convert Hexadecimal value String to ASCII value String in C++
- How to convert a 2D array to a CSV string in JavaScript?
- How to convert JSON string to array of JSON objects using JavaScript?
- How to assign values to an array with null/empty objects in JavaScript?
- How null is converted to String in JavaScript?

Advertisements