Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRyan Tung
Courses Plus Student 2,216 Pointsjavescript:bikeBell();
What does the () mean? What is the usage? and why I have to put () at behind? is that necessary?
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="charset" value="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=0.5, maximum-scale=0.5, minimal-ui">
<title>Car Sounds</title>
<!--Style Sheet link-->
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<!--Car image -->
<img src="images/bike.png" class="car" alt="car">
<!--Button-->
<a href="javascript:bikeBell();"><img src="images/bikeLock.png" alt="key"></a>
<!--Audio Files-->
<audio id="bikeBell" src="sounds/bikeBell.mp3" preload="auto"></audio>
<!--Javascript-->
<script type="text/javascript">
function bikeBell() {
document.getElementById('startCar').play();
}
</script>
</body>
</html>
2 Answers
Billy Bellchambers
21,689 PointsAll functions in JavaScript whether defined or not require the () this is used should you need to pass values into the function. and just part of the normal syntax.
By pass values to function see below.
function example(variable) {
console.log(variable);
}
//when called the function will use the value passed to it inside the function
example(10);
//this will use the value of 10 wherever variable appears inside the function, this is only an example as always your variables can be called anything, have as many as you like by separating them with comma's or use none at all. Like in this example but the () still need to be there even if passing no values
example(10) {
console.log(10);
}
//effect result like above
Hope this helps.
Happy coding!
Jennifer Nordell
Treehouse TeacherThose parentheses are an indication of a method or function. Actually a method is a function, it's just a function specifically attached to an object. Sometimes, as you probably know, a method/function will take arguments. If you don't have a parentheses it'll be looking for a variable. Ie xCount might be a variable that holds an int while xCount() might call a function that counts items in a list.
Ryan Tung
Courses Plus Student 2,216 Pointsthanks for the explanation! :D
Ryan Tung
Courses Plus Student 2,216 PointsRyan Tung
Courses Plus Student 2,216 PointsCool thanks for the explanation!