Check out a free preview of the full The Good Parts of JavaScript and the Web course

The "JavaScript Syntax" Lesson is part of the full, The Good Parts of JavaScript and the Web course featured in this preview video. Here's what you'd learn in this lesson:

JavaScript is syntactically a member of the C family. The main syntax difference between JavaScript and C is in the type system. Doug gives different JavaScript syntax examples including identifiers, comments, and operator use.


Transcript from the "JavaScript Syntax" Lesson

>> [MUSIC]

>> Speaker 1: JavaScript is syntactically a member of the C family.
>> Speaker 1: We've got identifiers which can start with letters or underbars or dollar signs. I recommend not starting or ending your property names with underbars or dollar signs, but they seem to be really popular. Dollar sign was added to the language for the benefit of machines for macro processors and code generators, things that wanted to be able to create names be guaranteed they wouldn't conflict with your names that they could use a dollar sign and that made them safe.

Unfortunately, some kids found out that you could have a function called dollar and went crazy with it. So, dollar functions are all over the place now.
>> Speaker 1: We have both formats for doing comments. I recommend using just these slashslash line format, because sometimes we want to use comments for commenting out code and regular expression literals can contain starslash and slashstar.

So weirdness can happen, if you're using a block comments. We have the same set of operators that you would expect to see in a C-like language. A few of them work a little bit differently that you need to be aware of. One big mistake in the language is that the plus operator does both addition and concatenation.

This is a bad habit JavaScript learn from Java. In Java, it's not so bad, because it strongly typed. So, you can predict which one it's going to do. JavaScript is loosely typed. So it's not until you go to do the plus, there it looks at the operands. And if they're both numbers it'll add them, otherwise, it'll convert them both into strings and concatenate them, which is bad.

For example, in a web application, you might have a form field when you ask the user to type a number into the field, you then wanna take that number out and add to something. Forgetting that the value you take out of a form element is always a string even though everything around it says, it's a number.

That's a really big source of confusion and you don't get an error you just get extremely bad behavior.
>> Speaker 1: So, we can convert and we can use the plus unitary operator to convert strings into numbers. And so, you often wanna do that is as a defensive thing. If you've got a value and you want to add it and you're concerned that it might not be a number, you can coerce it to be a number before you do the addition.

But if you do that, I recommend putting parens around it. Because otherwise, you end up with two plusses next to each other, which can look like another problem.
>> Speaker 1: We don't have integers formally. So you can divide two integers using the divide operator, but you're not guaranteed to get an integer result.

So, you need to be prepared for that. And because it's binary floating point, even the floating point result, you get might not be the one you would expect.
>> Speaker 1: The percent sign operator is the remainder operator not the modulo operator, which is a shame, because I think modulo is the more useful one.

The difference is in which sign it uses.
>> Speaker 1: We already talked about double equal and the problems with it. I recommend you always use triple equal instead, just because there are so many weirdness in it, things that are expected. There's a meme on YouTube called WAT. W-A-T. Has anybody seen WAT?

Yeah, it's a crackup. And mostly, it's playing fun with this. They take two things, which are wildly different and double equal them and then says, true and they go WAT? And they get a big laugh. So, don't use double equal. With the logical and operator works a little differently than it does in Java, because the operands do not need to be bullion, they only need to be bullish.

So if the first operand is truthy, the result is the value of the second operand. Otherwise, it's the result of the first operand. It does do the short circuiting though, so the second operand will only be evaluated if the first one was faulty and logical or works in a similar way.

>> Speaker 1: The exclamation point is the logical not operator, if the operand is truthy than the result is false. Otherwise, the result is true. If you have bang, bang twice, it will turn a bullish value into a Boolean.
>> Speaker 1: We have bitwise operators, but we don't have ints. So the way that works is we'll take the 64-bit binary floating point, turn it into a 32-bit signed integer.

Do the nasty to it and then convert it back. So in some languages, you'll see people doing a shift, because they think it's gonna be faster than a multiply. You shouldn't do that even in those languages, cuz compilers are smart enough. You should write the thing that you intend, so that someone reading the program will know what supposed to be happening.

The compiler will sort out the fastest way to do it. But in this language, definitely, you're not getting speed improvement by doing it the wrong way.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now