Warning: include(nectar/meta/meta-config.php) [function.include]: failed to open stream: No such file or directory in /home/content/18/9019618/html/v2014/wp-content/themes/salient/functions.php on line 4650

Warning: include() [function.include]: Failed opening 'nectar/meta/meta-config.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /home/content/18/9019618/html/v2014/wp-content/themes/salient/functions.php on line 4650
Beginner's Guide To JavaScript Objects And Functions - Part

Beginner’s Guide To JavaScript Objects And Functions – Part 3

By May 14, 2015 Blog No Comments
Beginner's Guide To JavaScript Objects And Functions - Part

In the previous post, we introduced you to JavaScript variables and showed you how to declare and use them. We also showed you how to properly write comments and access the JavaScript console. Today, we’re going to talk about JavaScript objects and functions. Let’s dive right in.

What Are JavaScript Objects?

You already learned that JavaScript variables are essentially data containers and that you can assign specific data value to a variable, as well as overwrite the value later on. However, what happens when you want to assign more values to a variable? That’s where JavaScript objects come in handy.

Beginner's Guide To JavaScript Objects And Functions - PartBuy Theme Open Demo

JavaScript objects are basically variables with multiple values. The values are written as name : value pairs (name and value separated by a colon). Compare this variable:
var person = "Steve Jobs";
with the following JavaScript object:
var person = {firstName:"Steve", lastName:"Jobs", age:50, eyeColor:"hazel"};
As you can see, JavaScript objects are variables made of multiple unordered variables called named values. All these named values (firstName, lastName….) are properties. In addition to properties, JavaScript objects can also have methods.

Object methods are object properties that contain function definitions, or simply put, actions that can be performed on objects. Take a look at this built-in method that transforms the greeting message we programmed in our last post into upper case:
var greetingMessage = "Welcome to AvaThemes!";
alert (greetingMessage.toUpperCase());

This will result in a pop-up alert, just like the last time, however, this time, all the letters in the message will be upper case.

What Are JavaScript Functions?

Functions are at the core of JavaScript, which is why it’s vital you understand what they’re all about. JavaScript functions are essentially sets of instructions, and they can be used in a variety of ways. The most basic way to define a function is by using the keyword function followed by the function identifier or the function name, followed by opening and closing parentheses, and opening and closing curly braces. In order to use a function, you have to call it by using its identifier followed by a set of parantheses and a semicolon. Let’s take a look at an example:
function sayHi(name) {
alert("Hi, "+name)
}
sayHi('Steve')

In this example, sayHi is the name of the function, and as you probably have already noticed, it follows the camelCase naming convention we mentioned in our previous post, which is to say that the same variable naming rules apply to functions as well.

Beginner's Guide To JavaScript Objects And Functions - PartBuy Theme Open Demo

Not only that, but JavaScript functions can be used as variables as well. For example:
var myFunction = function (a, b) {return a * b};
var x = myFunction(4, 3);
alert (x);

The last line will result in a pop-up alert displaying the value of the variable x (12) which is defined by the function called myFunction in the first line.

They say that the journey of a thousand miles begins with a single step, which is exactly what you just did. This beginner’s guide has provided an overview of JavaScript, one of the most powerful programming languages that can be found throughout the internet. While you’re still far away from being able to program the next Google Docs, you’re ready to tackle some of the more advanced JavaScript tutorials and courses.

Leave a Reply