1. Write your first Javascript function with Emojis
Learning about basic data structures in Javascript is a lifetime reward
So let's get right into the topic. Functions are a block of code that executes the commands or the logic it contains. There are two types of functions
Hard-coded functions
Dynamic Functions
First, let's take a look at Hard-coded Functions. Ever bought a baby doll that plays a song when you hit a button?
Play with the below makeFriends() function and let's analyze it below
Code Playground
Try with different emojis
Remove the double quotes and try to run the program
Notice here that every time you call the function it's simply gonna add those two emojis and logs it out no matter what. This is called a hard-coded function.
Analysis
Let's Analyse-it.
When you write your function you gonna write it by defining it as a "function" and you are gonna name it using camelCase, especially in JS. In Python, you will be using PascalCase. You can read more about it in detail here.
Step 1
In our case let's name it makeFriends() since this function makes everyone inside it friends by bringing them together. And add our lion and tiger emojis then returning it
function makeFriends() {
return "🦁" + "🐯"
}
Step 2
If we write our function without return we will get no output. As for javascript, we need to log it in the console for output.
console.log(makeFriends())
Output
"🦁🐯"
Whole Function
function makeFriends() {
return "🦁" + "🐯"
}
console.log(makeFriends())
Pat yourself on the back cause you just wrote your first javascript function and it works 🎉
In the next article, we will learn about dynamic functions 👋