Locked lesson.
About this lesson
What does JavaScript code look like?
Exercise files
Download this lesson’s related exercise files.
Syntax.docx58.7 KB Syntax - Solution.docx
58.9 KB
Quick reference
Syntax
JavaScript syntax is important.
When to use
Anytime you write JavaScript code, you need to follow the syntax rules.
Instructions
All JavaScript statements (lines of code) end in semi-colons.
Variables are named with lower case Camel Case (lowercase for the first word, uppercase for the second).
Quotation marks can be either double " or single '
Hints & tips
- JavaScript syntax is important!
- Lines in in semi-colons
- Variables are lower case Camel Case
- Quotation marks can be single or double
- 00:05 Okay, before we dive in and start learning hardcore JavaScript,
- 00:08 I wanna take just a couple of minutes and talk about syntax.
- 00:11 And syntax is basically the rules of how to write your computer code.
- 00:14 What does the code look like?
- 00:15 What are the different ways you do things?
- 00:18 So every programming language has its own rules.
- 00:20 This one uses colons at the end of every line.
- 00:23 This one uses semicolons at the end of every line.
- 00:25 This one uses commas at the end of every other line.
- 00:28 They're all different.
- 00:28 They all do things slightly differently and JavaScript is no difference.
- 00:31 So I wanna spend just a minute or
- 00:33 two talking about some of the most common syntax rules.
- 00:36 Now, you're gonna pick this stuff up as we move along, but here at the beginning,
- 00:40 I just wanna give a few very obvious ones and
- 00:43 spend just a couple of minutes talking about it.
- 00:44 So first of all, whenever we write a line of code in JavaScript,
- 00:48 it's called a statement, we're writing a statement of something.
- 00:50 And all statements end in semicolons, so
- 00:53 we have this script.js file earlier we could see, this ended in a semi-colon.
- 00:59 Every single statement you write in JavaScript will end in semi-colons.
- 01:02 So just burn that into your brain, you're always going to end in semi-colons.
- 01:06 And you're gonna forget from time to time.
- 01:07 You're gonna be writing code, and
- 01:08 you're gonna forget to put that semi-colon on the end, and it's not gonna work, and
- 01:12 you're gonna go, why isn't my code working?
- 01:14 You forgot the semicolon, happens all the time,
- 01:16 happens to me even after 20 years of doing this.
- 01:18 So that's the first thing.
- 01:20 Next, variables.
- 01:22 A variable, we haven't talked about, obviously.
- 01:23 We'll talk about variables later.
- 01:25 But variables, you call them like say,
- 01:29 myvariable equals something, whatever, right?
- 01:35 So how you name that variable in JavaScript ... First of all,
- 01:38 JavaScript is case sensitive.
- 01:40 And that means, for instance, we copy this and paste another copy.
- 01:46 So we have MyVariable, these are two very different variables, right?
- 01:50 Because this one has capital M and a capital V, and this one doesn't.
- 01:54 So JavaScript sees these as two different variables.
- 01:56 Some programming languages are case-insensitive,
- 01:59 where this would be the same variable.
- 02:01 Not JavaScript.
- 02:02 Capital letters and lowercase letters are very important in JavaScript.
- 02:05 So in JavaScript, the convention is to use something called camel case.
- 02:10 And this second one is camel case.
- 02:12 It has a capital M and a capital V, and they call it c camel case,
- 02:15 because camels have humps, a couple of humps.
- 02:18 And so, this capital M is a hump, and this capital V is a hump, that's camel case.
- 02:24 So in JavaScript, the convention is to use camel case, but to use a lowercase for
- 02:29 the first word, so lowercase my, uppercase variable.
- 02:33 That's how you do it in Javascript.
- 02:34 Now, this is valid too.
- 02:36 You can use an underscore to separate words in Javascript.
- 02:39 Most people just don't do that, though.
- 02:41 Sorta like the community of coders have decided that this is the way to do it,
- 02:45 lowercase camel case.
- 02:46 Some people like to do dashes with their variables.
- 02:49 You can't do a dash like that in JavaScript 'cause JavaScript sees that as
- 02:54 subtraction.
- 02:55 This is a subtraction sign in JavaScript, so it
- 02:57 thinks you're trying to subtract variable from my and it throws up an error.
- 03:02 So that's basically it for variables.
- 03:04 Next, strings.
- 03:06 This is a string here, this something right here.
- 03:09 We've created it with double quotation marks, right?
- 03:12 In JavaScript, you can use double quotation marks or you can
- 03:17 use single quotation marks like this, and it's the same thing.
- 03:23 So this something is the same as this something.
- 03:26 Double quotation marks versus single quotation marks, perfectly normal.
- 03:30 And there are instances where you might wanna use single quotation marks or
- 03:33 double quotation marks.
- 03:34 An we'll get into that as we go along and
- 03:35 you'll start to learn about different things like that.
- 03:38 So those are sorta the big, obvious syntax rules in JavaScript.
- 03:42 Like I said there's a bunch of other ones.
- 03:43 You'll pick them up as we go along and
- 03:44 I'll point them out from time to time as we move through this course, but
- 03:48 those are the ones to keep an eye on right off the bat.
- 03:51 This semi colon is the big one.
- 03:53 So in the next video I wanna talk about JavaScript output.
Lesson notes are only available for subscribers.