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 var
for compatibility with older browsers.
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¶
-
Variable names are case-sensitive (e.g.,
myVariable
is different frommyvariable
). -
Variable names can include letters (a-z), numbers (0-9), underscores (_), and dollar signs ($).
-
They cannot start with a number (e.g.,
person2
is allowed, but2person
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:¶
- 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"
- Manual Type Conversion: Developers can use builtin or custom functions for manual conversions.
- String to Number:
Convert strings to numbers using
Number()
,parseInt()
, orparseFloat()
.
var strNum = "20";
var num1 = Number(strNum);
var num2 = parseInt(strNum);
var num3 = parseFloat(strNum);
- Number to String:
Change numbers to strings by concatenating with an empty string, using
String()
, ortoString()
function.
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:
- 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.
- Strict Equal (===): Compares both value and data type, ensuring that both are identical.
- Not Equal (!=) and Strict Not Equal (!==): Similar to equal operators but negated.
- Greater Than (>): Checks if the left operand is greater than the right operand.
- Less Than (<): Checks if the left operand is less than the right operand.
- 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.
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.