Posts

Showing posts from February, 2018

Functions in JavaScript

Image
Functions in Javascript We're about to deep dive into Functions in JavaScript and, as you'll see, this is where you start to see some major differences from Java. I know because they have a similar name and syntax, it's easy to assume that the two languages are the same, but JavaScript is a lot less rigid in its form, which is very apparent in how Functions are constructed. Let's get into it! Function Declarations  Functions are what we would think of as methods in Java, a block of code that performs a task when called upon. We previously discussed how JavaScript doesn't require data types in variable declarations , and the same thing applies to functions . You don't have to specify the return type in the function's signature, nor do the parameters reference a data type. Function Declaration Syntax Declaring a function requires the "function" keyword, a name, the parameter(s), the keyword "return" and a return statement. ...

Loops in JavaScript

Image
It's time for another JavaScript lesson! This time we're looking at iterative statements, or loops that repeat a task until a condition is met. In JS, those statements come in the same two base formats as we've seen in Java, for and while . We'll look at the basic syntax of those two statements as well as variations like forEach , for...in , for...of , do...while , and loop control statements like break and continue . As a bonus, even though these aren't concepts we've covered in Java, they apply to it as well, so double the knowledge! For Loops Syntax We know the basics of how a for loop works from our experience with Java. Fortunately, the syntax is very similar. for ([initial expression]; [condition]; [increment expression]) {      statement; } The major difference between JS and Java is that you don't have to identify the variable type in the initial expression when you declare the starting point. What About The Enhanced For Loop? Ah...

Conditional Statements in JavaScript

Image
Welcome back, friends! In this week's language lesson, we're looking at how conditional statements are written in JavaScript. A lot of what I cover in this post will be similar to Java, with a few exceptions, like the comparison operators. We will take a look at those first before jumping into conditional statement syntax. JavaScript Comparison Operators In Java, we learned to use == (equal) or != (not equal) for comparisons of the same data type, with the exception being .equals for strings. However, in JavaScript that is not the case. There are actually two versions of the equal/not equal operators. Abstract Equality (==) Using double equals for comparison in JavaScript is called abstract equality, which means that if you are comparing values of different data or object types, JS will attempt to convert them to similar types through "type coercion" before comparing. Type coercion - The process of converting value from one type to another (such as st...

Constants and Variables in JavaScript

Intro To This Week's Topic We're talking about Constants and Variables in JavaScript (JS) and taking a look at the syntax and rules for declaring them. The syntax for JavaScript is based off of Java, but is also influenced by Awk, Perl, and Python. JS is  case-sensitive and uses the Unicode character set. Therefore, a variable named number is different from one named Number . In JS, instructions are called statements and are separated by a semicolon (;) , like in Java. While a semicolon is not necessary if statements are written on separate lines, it is best practice to do so. There are two types of declarations made in JS; Variables  var , and Constants  const . We will dive into each one below. Variables ( var ) Naming Variables A JavaScript variable's name, or identifier, must adhere to certain rules: It must start with a letter, underscore (_), or dollar sign ($). Characters after the first can be digits (1-9). Because JS is case-sensitive, A-Z are...