Skip to content

Class 1 - var, data type, comparison operator

At the very first class, we discuss about the below topics:

  • Variable in JavaScript

  • Data Types in JavaScript

  • Data Type Conversion in JavaScript
  • Comparison Operators in JavaScript

The ultimate goal of this course is to gain proficiency in the Google Earth Engine JavaScript API, from basic to advanced concepts. Recognizing the importance of understanding fundamental programming concepts as a prerequisite, the course begins with JavaScript to build a solid foundation in programming. This approach ensures that participants are well-equipped with the necessary skills before exploring the intricacies of the Google Earth Engine JavaScript API. Let's get started!

Variable

In programming, a variable is like a storage container that holds a value. It allows you to store and manipulate data within your code.

Declaring Variable in JS

In JavaScript, you can declare a variable using the var, let, or const keyword. For GEE (Google Earth Engine), it's recommended to use varfor compatibility with older browsers.

var myVariable = 42; // Declaration and assignment

In the above example, myVariable is declared using the var keyword and assigned the value 42. This variable can now be used to store and manipulate data in the program.

How RAM Handles Variable Addresses

Behind the scenes, when a variable is declared, the computer's memory (RAM - Random Access Memory) plays a crucial role. RAM is a form of volatile memory that stores data and machine code currently being used and processed by the computer.

When a variable is declared, a specific portion of the computer's memory is allocated to store its value. The variable name acts as a reference or label for that memory location. This memory location has a unique address that the program uses to retrieve or update the stored value.

Variable Naming Rules

  1. Variable names are case-sensitive (e.g., myVariable is different from myvariable).

  2. Variable names can include letters (a-z), numbers (0-9), underscores (_), and dollar signs ($).

  3. They cannot start with a number (e.g.,person2 is allowed, but 2person is not allowed).

Basic Data Types

Text/String: - Used for representing textual data. - Declared using single (') or double (") quotes.

Number: - Used for representing numerical data, both integers and floating-point numbers.

Boolean: - Represents a logical entity and can have only two values: true or false.

Certainly! Let's simplify and make the explanation more concise:

Certainly! Let's include toString() in the explanation:

Data Type Conversion in JavaScript:

  1. Automatic Type Conversion (Coercion): JavaScript automatically converts data types during operations.
var num = 5;        
var str = "10";//10 is stroed as text      
var result = num + str;  //convert the data type of 5 from number to string, and then add with "10"
console.log(result);  // "510"
  1. Manual Type Conversion: Developers can use builtin or custom functions for manual conversions.
var strNum = "15";
var num = Number(strNum);  
console.log(num);  // 15
  1. String to Number: Convert strings to numbers using Number(), parseInt(), or parseFloat().
var strNum = "20";
var num1 = Number(strNum);       
var num2 = parseInt(strNum);     
var num3 = parseFloat(strNum);   
  1. Number to String: Change numbers to strings by concatenating with an empty string, using String(), or toString() function.
var num = 25;
var str1 = num + "";       
var str2 = String(num);
var str3 = num.toString();     

Using toString() is another way to convert a number to a string, providing flexibility and options for developers.

Comparison operator in JavaScript

In JavaScript, comparison operators are used to compare values and return a Boolean result (true or false). Here's a brief overview of some common comparison operators:

  1. Equal (==): Checks if two values are equal, but it performs type coercion, meaning it may convert the values to the same data type before making the comparison.
5 == '5'; // true (coerced equality)
  1. Strict Equal (===): Compares both value and data type, ensuring that both are identical.
5 === '5'; // false (strict equality)
  1. Not Equal (!=) and Strict Not Equal (!==): Similar to equal operators but negated.
5 != '5';  // false (coerced inequality)
5 !== '5'; // true (strict inequality)
  1. Greater Than (>): Checks if the left operand is greater than the right operand.
10 > 5; // true
  1. Less Than (<): Checks if the left operand is less than the right operand.
3 < 7; // true
  1. Greater Than or Equal (>=) and Less Than or Equal (<=): Check if the left operand is greater than or equal to, or less than or equal to, the right operand.
8 >= 8; // true
4 <= 2; // false

These comparison operators are often used in conditional statements and expressions to control the flow of a program. They play a crucial role in decision-making processes.

In this course, we will focus only those parts of JS which are required to learn GEE JS API. If you are interested to learn JS you can find at w3schools.com, Mozila foundation, Freecodecamp.