Basic JavaScript! - HeghineB/javascript-1-homework GitHub Wiki

Welcome to the javascript-1-homework wiki!

Welcome to JS world

Comments

Comments are lines of code that JavaScript will intentionally ignore. Comments are a great way to leave notes to yourself and to other people who will later need to figure out what that code does. There are two ways to write comments in JavaScript:

  • Using // will tell JavaScript to ignore the remainder of the text on the current line: //this is an in-line comment
  • You can make a multi-line comment beginning with /* and ending with */: /*this is a multi-line comment*/

Declare JavaScript Variables

In computer science, data is anything that is meaningful to the computer. JavaScript provides seven different data types which are:

  • undefined
  • null
  • boolean
  • string
  • symbol
  • number
  • object Variables allow computers to store and manipulate data in a dynamic fashion. They do this by using a "label" to point to the data rather than using the data itself. Any of the seven data types may be stored in a variable. In JavaScript, we end statements with semicolons.

Variable names can be made up of numbers, letters, and $ or _, but may not contain spaces or start with a number.

Example: var myName;

Storing Values with the Assignment Operator

In JavaScript, you can store a value in a variable with the assignment operator. Assignment always goes from right to left. Everything to the right of the = operator is resolved before the value is assigned to the variable to the left of the operator.

Exaple: var a = 7; var b = a; This assigns 7 to var a and then resolves var b to 7 again and assigns it to var a.

Initializing Variables with the Assignment Operator

It is common to initialize a variable to an initial value in the same line as it is declared.

Example: var a = 9;

Understanding Uninitialized Variables

When JavaScript variables are declared, they have an initial value of undefined. If you do a mathematical operation on an undefined variable your result will be NaN which means "Not a Number". If you concatenate a string with an undefined variable, you will get a literal string of "undefined".

Example: var a; var b; var c; these are undefined variables

Understanding Case Sensitivity in Variables

In JavaScript all variables and function names are case sensitive. This means that capitalization matters.

MYVAR is not the same as MyVar nor myvar. It is possible to have multiple distinct variables with the same name but different casing. It is strongly recommended that for the sake of clarity, you do not use this language feature. Best Practice Write variable names in JavaScript in camelCase. In camelCase, multi-word variable names have the first word in lowercase and the first letter of each subsequent word is capitalized.

Example: var studlyCapVar = 10; var properCamelCase = "A String"; var titleCaseOver = 9000;

Add Two Numbers with JavaScript

The number is a data type in JavaScript which represents numeric data. JavaScript uses the + symbol as addition operation when placed between two numbers.

Example: var sum = 10 + 10;

Subtract One Number from Another with JavaScript

We can also subtract one number from another. JavaScript uses the - symbol for subtraction.

Example: var difference = 45 - 33;

Multiply Two Numbers with JavaScript

We can also multiply one number by another. JavaScript uses the * symbol for multiplication of two numbers.

Example: var product = 8 * 10;

Divide One Number by Another with JavaScript

We can also divide one number by another. JavaScript uses the / symbol for division.

Example: var quotient = 66 / 33;

Increment a Number with JavaScript

You can easily increment or add one to a variable with the ++ operator. i++; is the equivalent of i = i + 1; Note The entire line becomes i++;, eliminating the need for equal sign.

Example: myVar++;=myVar = myVar + 1

Decrement a Number with JavaScript

You can easily decrement or decrease a variable by one with the -- operator. i--; is the equivalent of i = i - 1; Note The entire line becomes i--;, eliminating the need for the equal sign..

Example: `myVar--; myVar = myVar -1

Create Decimal Numbers with JavaScript

We can store decimal numbers in variables too. Decimal numbers are sometimes referred to as floating point numbers or floats.

Note Not all real numbers can accurately be represented in floating point. This can lead to rounding errors.

Example : var myDecimal = 5.7;

Multiply Two Decimals with JavaScript

In JavaScript, you can also perform calculations with decimal numbers, just like whole numbers. Let's multiply two decimals together to get their product.

Example : var product = 2.0 * 2.5;

Divide One Decimal by Another with JavaScript

Now let's divide one decimal by another.

Example: 4.4 / 2.0

Finding a Remainder in JavaScript

The remainder operator % gives the remainder of the division of two numbers. Usage In mathematics, a number can be checked to be even or odd by checking the remainder of the division of the number by 2.

Example: var remainder = 11 % 3; 17 % 2 = 1 (17 is Odd) 48 % 2 = 0 (48 is Even) Note The remainder operator is sometimes incorrectly referred to as the "modulus" operator. It is very similar to modulus, but does not work properly with negative numbers.

Compound Assignment With Augmented Addition

n programming, it is common to use assignments to modify the contents of a variable. Remember that everything to the right of the equals sign is evaluated first, so we can say: myVar = myVar + 5; to add 5 to myVar. Since this is such a common pattern, there are operators which do both a mathematical operation and assignment in one step. One such operator is the += operator.

Example: `var a = 3; var b = 17; var c = 12;

a += 12; b += 9; c += 7;`

Compound Assignment With Augmented Subtraction

Like the += operator, -= subtracts a number from a variable. myVar = myVar - 5; will subtract 5 from myVar. This can be rewritten as: myVar -= 5;

Example: `var a = 11; var b = 9; var c = 3;

a -= 6; b -= 15; c -= 1; `