Using JavaScript's Slice Method to Carve a Turducken
Tags: javascript, tutorial, beginners
Published: 2021-11-17 15:39:29

It's Thanksgiving time in the US. So lets Slice the bird. This year we're not settling for a turkey, lets go for the excessive Turducken. A Turducken is a deboned chicken cooked inside a deboned duck that is cooked inside a deboned turkey.

## Ingredients

First gather all the ingredients or variables.

```Javascript

let bird1 = "Turkey"; 
let bird2 = "Duck";
let bird3 = "Chicken";

``` 

## Slice

We use a basic JavaScript slice to carve the turkey. The `slice()` method extracts a section of a string and returns it as a new string. Use parameters to specify the part of string to be extracted. 

The parameters are the start point and the end point, The slice happens before the endpoint. That character is not included in the new string.

The first character is at position zero, the rest are off by one. To make a Turducken we need to remove the first three letters from Turkey or Position 0 to 3 wrote as `bird1.slice(0, 3)`. 

```Javascript
let bird1 = "Turkey";

let carveFirstBird = bird1.slice(0, 3);

```
Will yield "Tur."

Next we need the Duck. We want the whole duck, or whole string, so we only need the first parameter. If you don't add an endpoint the whole string will be sliced.

`bird2.slice(0);`

```Javascript
let bird2 = "Duck";
let carveSecondBird2 = bird2.slice(0).toLowerCase();

```
This will give us the "duck" 

Finally we need the chicken but how do we get the end of the string, negative numbers. to get the 'en' we slice like this, ` bird3.slice(-2)` This will slice the string starting at two characters from the end.

```Javascript
let bird3 = "Chicken"; 
let carveThirdBird = bird3.slice(-2);
```
We have the "en" from Chicken. Now to put them together.

## Combine Ingredients

Make dinner by putting it all the birds together.  

```Javascript

let dinner = carveFirstBird + carveSecondBird + carveThirdBird;
document.write(dinner); // Turducken
```

This give us Turducken.

## Recipe

```Javascript
let bird1 = "Turkey"; 
let bird2 = "Duck";
let bird3 = "Chicken";

let carveFirstBird = bird1.slice(0, 3);
let carveSecondBird = bird2.slice(0).toLowerCase();  
let carveThirdBird = bird3.slice(-2);

let dinner = carveFirstBird + carveSecondBird + carveThirdBird;
 document.write(dinner);
```

## If you love that, try this

There's also a Gooducken. This recipe replaces the turkey with a goose. For this example we can use another way to get the end of the string for the dinner.
We slice bird1 the same as above. Instead of using the whole string for bird2 we only get the first three letters. So we slice the duck the same way as we did for Goose.

```Javascript

let bird1 = "Goose"; 
let bird2 = "Duck";
let bird3 = "Chicken";

let carveFirstBird = bird1.slice(0, 3);
let carveSecondBird2 = bird2.slice(0, 3).toLowerCase();


``` 
Then how do we get the "K" we pull it from the chicken but use another way. We slice the fourth position, but fifth character, and  just go to the end of the string. ` bird3.slice(4);` If we don't add an endpoint it will slice everything starting at the start point.

```Javascript

let carveThirdBird = bird3.slice(4);
```
gives us "ken". Dinner goes together same as before.

```Javascript

let dinner = carveFirstBird + carveSecondBird + carveThirdBird;

```
Gives us Gooducken.

That's how to carve with JavaScript. I hope you enjoyed this look at Slice. No, I have never tried a turducken in real life.

{% twitter 1460999452592947203 %}