💥A JavaScript function is defined with the function keyword,
followed by a name, followed by parentheses ()
The code to be executed, by the function, is placed inside curly brackets: {}
👊function
name(parameter1, parameter2, parameter3) {
code to be executed
}
💥 We can use function different ways
👊<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World!";
}
myFunction();
👊<script>
function myFunction(name) {
return "Hello " + name;
}
document.getElementById("demo").innerHTML = myFunction("John");
</script>
💥OBJECT
👊 <script>
var person = {
firstName : "John",
lastName : "Doe",
age : 50,
eyeColor : "blue"
};
document.getElementById("demo").innerHTML =
person.firstName + " is " + person.age + " years old.";
</script>
output John is 50 years old
👊<script>
var person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : function() {
return this.firstName + " " + this.lastName;
}
};
document.getElementById("demo").innerHTML = person.fullName();
</script>
output John Doe
No comments:
Post a Comment