Kebab-case refers to a programming variable naming convention in which a developer replaces spaces between words by a dash.
For Example
Kebab case: "the-quick-brown-fox-jumps-over-the-lazy-dog"
hello-world first-project
- One of its most common uses is for separating words in a URL; take a look at the URL for this post.
- However, dashes in variables can sometimes cause batch scripts and other programs, so be careful.
Here's the helper function for convert into kebab case in javascript
const toKebabCase = str => str && str .match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g) .map(x => x.toLowerCase()) .join('-');
console.log(toKebabCase("Hello World")); // hello-world
Lodash KebabCase
Converts string to kebab case
Arguments
[string=''] (string)
: The string to convert.
Returns
(string)
: Returns the kebab cased string.
_.kebabCase('Foo Bar'); // => 'foo-bar' _.kebabCase('fooBar'); // => 'foo-bar' _.kebabCase('__FOO_BAR__'); // => 'foo-bar'
In JavaScript variable names cannot have a hyphen, it results in an error.
let my-name = "Adam" Uncaught ReferenceError: Invalid left-hand side in assignment
JavaScript parses hyphen as a Subtraction Operator (-).We can use hyphens in object property names.