Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax:
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples 
4.1.
Converting a Regular String to Kebab Case:
4.2.
JavaScript
4.3.
Creating URL Slugs:
4.4.
JavaScript
4.5.
Handling Strings with Punctuation and Spaces:
4.6.
JavaScript
4.7.
Standardizing CSS Class Names:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.kebabCase() handle non-alphanumeric characters?
5.2.
Can _.kebabCase() handle camelCase and snake_case strings?
5.3.
Is _.kebabCase() suitable for all string conversion needs?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.kebabCase() Method

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In web development and programming, converting strings to kebab case (also known as spinal case or dash case) is a common requirement, especially for URL slugs, CSS class names, and certain JavaScript naming conventions. Lodash's _.kebabCase() method offers a convenient way to transform strings into this format. 

Lodash _.kebabCase() Method

This method is particularly useful for standardizing text strings for consistency across web and application development.

Why This Function is Used

The _.kebabCase() function is used to convert strings into kebab case, where words are lowercase and separated by hyphens. This format is commonly used in various programming contexts, including web development, where it's important for elements like URL slugs, CSS classes, and identifiers to adhere to a standard, readable, and URL-friendly format.

Syntax, Parameter and Return Value

Syntax:

 _.kebabCase([string=''])

Parameters:

[string=''] (string): The string to convert.

Return Value: 

(string) - Returns the kebab cased string.

Examples 

Converting a Regular String to Kebab Case:

  • JavaScript

JavaScript

var _ = require('lodash');

var text = 'Hello World';

console.log(_.kebabCase(text));
You can also try this code with Online Javascript Compiler
Run Code

Output: 

'hello-world'

Demonstrates converting a space-separated string to kebab case.

Creating URL Slugs:

  • JavaScript

JavaScript

var title = 'Lodash Library: A Comprehensive Overview';

var slug = _.kebabCase(title);

console.log(slug);
You can also try this code with Online Javascript Compiler
Run Code

Output:

 'lodash-library-a-comprehensive-overview'


Shows converting a title to a URL-friendly slug in kebab case.

Handling Strings with Punctuation and Spaces:

  • JavaScript

JavaScript

var phrase = 'JavaScript: The Good Parts!';

console.log(_.kebabCase(phrase));
You can also try this code with Online Javascript Compiler
Run Code

Output:

 'java-script-the-good-parts'


An example of kebab casing a string with various punctuation marks and spaces.

Standardizing CSS Class Names:

  • JavaScript

JavaScript

var className = 'MainContainer Highlighted';

console.log(_.kebabCase(className));
You can also try this code with Online Javascript Compiler
Run Code

Output:

 'main-container-highlighted'

Demonstrates converting class names to kebab case for use in CSS.

Frequently Asked Questions

How does _.kebabCase() handle non-alphanumeric characters?

_.kebabCase() removes non-alphanumeric characters and uses hyphens to separate words.

Can _.kebabCase() handle camelCase and snake_case strings?

Yes, _.kebabCase() can convert strings from camelCase, snake_case, and other formats into kebab case.

Is _.kebabCase() suitable for all string conversion needs?

_.kebabCase() is ideal for converting strings to kebab case. For other naming conventions like camel case or snake case, different Lodash methods or custom logic would be required.

Conclusion

Lodash's _.kebabCase() method provides an efficient and straightforward way to convert strings of various formats to kebab case. It's particularly useful for creating URL slugs, standardizing CSS class names, and ensuring text string consistency in web and application development.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass