Skip to content

1.13 Summary

You have reached the end of section 1. Congratulation! Do not underestimate the importance of the basics, which we have established in this section. Once you grasped the essentials, it will be much easier for you to learn new things and to build on what you have already mastered. You can revise everything we have learned here. It is already too much. And guess what you have understood it all. That is great!

What is JavaScript?

  • It is a scripting language. You can write programs and run them on any browser by using JavaScript.

  • You can create a JavaScript file by simply creating a text file with js extension. For example, script.js.

  • To run the JavaScript file, you must link it to an HTML file as follows:

<!DOCTYPE html>
  <html lang="en" dir="ltr">
    <head>
      <meta charset="utf-8">
      <title>Your first JavaScript file<title>
    </head>
    <body>
      <script src="script.js"></script>
    </body>
  </html>
  • You can test JavaScript code in the console, but the code goes away as soon as you refresh the page.

Displaying on screen

In JavaScript, you can display messages on the screen by different methods. Some of them are listed below:

  1. Alerts:alert('message');
  2. Logging: console.log('message');

Comments

  • Use //Your comment here for single-line comments.

  • Use /* Your comment here */ for multi-line comments.

Math operations

You can perform math operations easily in JavaScript.

  1. Addition: 1 + 1, which is equal to 2
  2. Subtraction: 5-2, which is equal to 3
  3. Multiplication: 8*9, which is equal to 72
  4. Division: 50/20, which is equal to 2.5

Comparison Operators

  • <: less than
  • <=: less than or equal to
  • >: greater than
  • >=: greater than or equal to
  • ==: equal to (in terms of value only)
  • ===: equal to (in terms of value and type)
  • !=: not equal (in terms of value)
  • !==: not equal (in terms of value and type)

Logical Operators

  • &&: and
  • ||: or
  • !: not

Variables

  • A variable is a container of data. It has a name and a value.

  • We declare a variable by using var keyword.

  • The initial value of any variable declared with var is undefined.

  • var age = 100; herein, we defined a variable called age. We assigned a value of 100 to it.

Strings

  • A string is a series of characters. For instance, "Hello".

  • A string is either enclosed within single or double quotation marks.

  • You can get the number of the string characters by using string.length.

  • You can convert a string to uppercase by using string.toUpperCase().

  • You can convert a string to lowercase by using string.toLowerCase().

  • You can find the position of a character in a string by using string.indexOf('the-character').

  • You can join/concatenate two strings together by using string1.concat(string2); or string1 + string2

Boolean

  • It is a data type in JavaScript. It can be either true or false.

  • null, undefined, and zero are false in JavaScript.

undefined VS null

  • undefined: the variable has not been assigned any value yet.

  • null: the variable is declared, and has been assigned the value of null/nothing. null indicates a lack/absence of a value.

typeof

  • typeof returns the type of data contained in a variable.

  • The basic data types in JavaScript: number, string, boolean, undefined, and null.

  • Example:typeof "hello";. It is a string.

Conditional Statements

  • You can write a simple if statement as follows:
if(condition){
  // Tasks to be executed
}
  • You can write if/else statement as follows:
if(condition){
  // Tasks to be executed
  // if the condition is true
}else{
  // Tasks to be executed
  // if the condition is false
}
  • You can write if/else if/else statement as follows:
if(condition1){
  // Tasks to be executed
  // if the condition1 is true
}else if(condition2){
  // Tasks to be executed
  // if the condition2 is true
}else if(condition3){
  // Tasks to be executed
  // if the condition3 is true
}else{
  // Tasks to be executed
  // if all conditions are false
}
  • You can write a simple if statements in a short way by using Ternary Operator:
var mark = 70;
var pass =  mark > 60 ? true : false;
// condition ? value_if_true : value_if_false.

Loops

  • We use loops to perform repeated tasks.

for loop

  • The syntax of JavaScript for loop is:
for(var initial; condition; initialUpdate){
  // Tasks to perform.
}

while loop

  • The syntax of JavaScript while loop is:
while(condition){
  // Tasks to perform.
}

break

  • You can break out of the current loop by using break.

continue

  • You can skip the current iteration in the loop by using continue

When to use while loop?

Use the while loop when you do not know the number of the required iterations.


That is great! Let's move to the next section! Are you ready?! I am sure, you are :)