در حال بارگذاری ...

Running alert() basics0:00

I think a great place for us to get started is by just comparing some of the syntax and structure and even a little bit of scope between php and JavaScript, because there are a lot of similarities, there are also a lot of differences. And of course, this is just going to be a cursory look at those things. We have this whole series to dive deeper into those subjects. So for now, we're just going to write some code. We are going to execute a function called alert. Now, this is a function given to us by the browser, and the browser gives us a lot of things. But for right now, we just need to focus on this alert function. And all we need to do is supply a string value so that it will display that inside of the browser. There we have an alert box. We have the message that we passed to it, hello world, and that's that.

Strings and semicolons0:47

There we have an alert box. We have the message that we passed to it, hello world, and that's that. Now, there are a few things that I want to talk about here. First of all, this looks exactly like PHP, and the primary reason for that is because both PHP and JavaScript have a C style syntax. So there's a lot of similarities there. But there's two things. The first is that we have a string that is created by using single quotes, but we can also do this. We can use double quotes. And I know you're thinking we can do that in PHP. And yes, but in JavaScript, there is no difference between single quote strings and double quote strings.

And I know you're thinking we can do that in PHP. And yes, but in JavaScript, there is no difference between single quote strings and double quote strings. There's nothing special about them. They create strings. That's it. So you can use them interchangeably. So ideally, you know, pick one and stick with it. But there are some benefits to using one over the other. Like, for example, if you need to use an apostrophe inside of a string, then you might want to create that string using double quotes. However, if you need to use a double quote inside of a string, then you can create your string using single quotes.

Like, for example, if you need to use an apostrophe inside of a string, then you might want to create that string using double quotes. However, if you need to use a double quote inside of a string, then you can create your string using single quotes. But of course, if you need to use a single quote character inside of a string created with single quotes, you have to escape that with a slash. Now, the second thing is that this statement ends with a semicolon. But in JavaScript, semicolons are optional. You can actually write your code without a single semicolon. I don't recommend it because there are some circumstances where you will end up with some results that you didn't expect. And besides, you are used to using a semicolon at the end of your statements. So just follow through with that in your JavaScript. It will save you some headache.

Defining JavaScript functions2:31

So just follow through with that in your JavaScript. It will save you some headache. So with that, let's move on and let's create a function called helloWorld. We will paste in our call to alert. But let's simplify this so that it just says helloWorld. And once again, this is very much like php. Our function begins with the function keyword followed by the name of the function. We have a set of parentheses for the parameter list and then our function body uses curly braces. As I said, JavaScript and php have a C style syntax. So there is a lot of similarity between those.

As I said, JavaScript and php have a C style syntax. So there is a lot of similarity between those. But now let's do this. Let's write another function that we would use to add two values together. So we will simply return a + b. So here we can see that the parameters are inside of the parentheses just like they would in php. But notice that the parameters do not begin with a $ sign. That's because in JavaScript, variables are just an identifier. We don't prepend them with a $ sign, even though you can because a $ sign is a valid first character for parameters and for variables.

We don't prepend them with a $ sign, even though you can because a $ sign is a valid first character for parameters and for variable names. However, I recommend that you do not do that because the $ sign is typically left for jQuery. It has a special meaning there. And even today, people see a $ sign and they think, oh, this is something to do with jQuery. So our variable names, our parameter names do not have a $ sign. Also something to point out, the casing that we use is camelCase. Now, it is technically correct to use underscores in your variable names. That's perfectly fine. However, it does not follow the convention in JavaScript.

That's perfectly fine. However, it does not follow the convention in JavaScript. And I'm of the belief that you follow the conventions of whatever language that you are using. So because this is JavaScript, we use camelCase. Also, if you have a function that needs to return a value, we have the return keyword that we use to return that value. And there we go. Other than those very specific things about parameter names and variable names, it's exactly like PHP. Let's do this. Let's divide A and B so that we will return A divided by B. But you know what happens if B is zero?

Conditionals and errors4:49

Let's divide A and B so that we will return A divided by B. But you know what happens if B is zero? Because we can't divide by zero. So we can have an if statement. If B equals zero, then, well, we need to do something to show that that's an error. So we can throw a new Error. We can even have a message that says cannot divide by zero. Now, JavaScript would throw its own error if we tried to divide by zero. So doing something like this isn't really necessary. However, it is for explaining the structure and syntax of JavaScript.

So doing something like this isn't really necessary. However, it is for explaining the structure and syntax of JavaScript. Because here we have an if statement, we have a condition, and then we have the code block for that if statement. So it is exactly like it is in PHP. If we wanted to use an else, we could just tack that on. And there we go. But now let's do this. Let's say that we're going to have a function that says sayHello. We will provide a name. And then this function will simply greet whatever name that we provide.

We will provide a name. And then this function will simply greet whatever name that we provide. But let's write this like we would in php. So I'm even going to use a dollar sign for the variable. So we would have our $message equals. Then we would have our string of 'hello'. We would then concatenate that with the $name variable so that we could then return the $message. Or if we wanted to alert that, we could do that as well. And maybe that's what we need to do. OK, so this would be valid php.

Concatenation and scope6:18

And maybe that's what we need to do. OK, so this would be valid php. But we would run into a couple of issues here. First, to concatenate strings, we don't use a dot. We use a plus sign. So in JavaScript, the plus sign pulls double duty. It's used for adding numbers. It's also used for concatenating strings because, well, adding strings together is concatenation. So it kind of makes sense there. The second thing is our message variable.

So it kind of makes sense there. The second thing is our message variable. By just specifying a variable and assigning a value to it, we have created a global variable. So we could do something like this to where we would call, say hello. We would pass in the name of Jim. And then inside of the browser, we see hello, Jim. The reason is because we created a global variable here and we want to avoid that as much as possible. So instead, we want to explicitly tell JavaScript that we want to create a variable. Now, there are different ways that we can create variables. In fact, there are essentially three.

Now, there are different ways that we can create variables. In fact, there are essentially three. But using the var keyword is going to create a variable that works like variables in PHP. So by creating a variable using the var keyword, you have created a variable that can be used anywhere within the function that it is defined. You can also create global variables using the var keyword. You just need to do that outside of any function. And you can access global variables by just referencing the name of the variable wherever you want because it's global. So unlike PHP, where you would have to include the globals that you want to use within a function, or at least depending upon your configuration, you don't have to do that with JavaScript. A global is a global and you use it wherever you want. Unlike PHP, you can declare a variable and then assign it a value later.

A global is a global and you use it wherever you want. Unlike php, you can declare a variable and then assign it a value later. And there are some cases where you would want to do that. But typically, at least from my experience, anytime that I create a variable, I try to initialize it with some kind of value. So you are already used to doing something like that. So I recommend following that pattern. But you can create a variable and then assign it a value later. So we essentially have two statements. We have a variable declaration statement, then we have a variable assignment statement. Now, there's one other thing that I want to talk about, and that is, well, it's I'm going to say it incorrectly, but it gets the point across in that everything in JavaScript is an object.

Objects and dot syntax8:41

We have a variable declaration statement, then we have a variable assignment statement. Now, there's one other thing that I want to talk about, and that is, well, it's I'm going to say it incorrectly, but it gets the point across in that everything in JavaScript is an object. It's technically not, but you can use everything as an object. So we have a string value, this message. If we wanted to call the trim method on that variable, we can, and that will trim up any leading or trailing whitespace. If we wanted to convert it to uppercase, we could call the toUpperCase method. But the thing I want to point out is the operator that we use. In PHP, you would use the arrow. JavaScript just uses the dot. So you have object.property or object.method, and that's how you work with objects.

JavaScript just uses the dot. So you have object.property or object.method, and that's how you work with objects. And I think that's, well, I think that sums everything up. The overall structure of the language is very similar to PHP. The syntax, for the most part, is very similar. Scope is kind of similar, but it also depends upon how you create variables, which is something we will talk about in a later episode. But I think the next thing that we should probably talk about are the types of data that JavaScript provides, and we will look at that in the next episode.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟