Hey everyone! Today we’re checking out JavaScript Syntax. The grammar and rules JavaScript uses to work its magic!
Don’t worry, I’ll keep it simple. Let’s get started! First up, variables!
Variables store data, kinda like labeled boxes where you keep stuff. This is how we make variables: We could use "var" or "let" to make them. But, think of "var" as the old-school way of creating variables, and "let" as the modern way.
So lets stick with "let". Then, we assign values like this: x = 5, and y = 6. And z?
Well, that’s just x + y or … 11! Now let’s talk more about values. Fixed values, or values that you provide in the script, are called Literals in JavaScript.
There are different types of Literals for different data types, but lets look at 2 for now. Numbers and Strings. Numbers don’t need quotes, but strings, which are just text go in single or double quotes.
Next up, operators! Arithmetic operators do math: like adding, subtracting, and multiplying. Here, 5 + 6 adds up to 11, and then multiplies by 10.
That’s 110! Math is easy in JavaScript! Now: You may also have noticed a = operator in the previous example.
This is an assignment operator. It assign values to variables. In this case, it assigns our variable x the value of the calculation.
110! An expression is a combination of values, variables, and operators that gets evaluated. Here are two expressions: So, 5 * 10 gets evaluated to "50", and "John" + " " + "Doe" creates "John Doe".
See? Strings can add up too! Comments let you write notes in your code.
JavaScript ignores comments, so you can explain what your code does! Use // for single-line comments, /* */ for multi-line notes. Super helpful for you or other people in the future who are trying to read your code and figure out what each part does!
How do you name your variables? JavaScript names, which are called identifiers, follow some rules: Start with a letter, $, or _ You also can't use JavaScript Keywords. Keywords are used to identify actions to be performed, like we've used "var" and "let" in the examples.
So basically: No numbers at the start, no keywords. And names are case-sensitive! See?
lastName with uppercase N and lastname with lowercase n are two totally different variables. Always watch your cases! Speaking of Cases, lets look at naming conventions!
JavaScript developers love camel case where names start lowercase, but each word after starts with an uppercase letter. Other naming conventions exist, like first_name or FirstName with uppercase F and N, but stick to camel case for JavaScript! Awesome!
Now you know the basics of JavaScript Syntax: variables, literals, operators, and more. Thanks for watching! Like and subscribe to get notified about our latest videos!
For more coding tips and tricks, keep it locked right here on W3Schools. com! Don't forget to try out what you've learned in our interactive editor.
Happy coding!