Reference
Codecademy. Retrieved from https://www.codecademy.com/
JavaScript is an object-oriented programming language that powers the dynamic behavior on most websites. Alongside HTML and CSS, it is a core technology that makes the web run.
Console
The console.log()
method is used to log or print messages to the console. It can also be used to print objects and other info.
console.log('Hi there!');
// Prints: Hi there!
Comments
In JavaScript, single-line comments are created with two consecutive forward slashes //
.
// I'm a Single Line Comment
And multi-line comments are created by surrounding the lines with /*
at the beginning and */
at the end. Comments are good ways for a variety of reasons like explaining a code block or indicating some hints, etc.
/*
I'm
Multiple
Line
Comment
*/
Data Types
Data types are the classifications we give to the different kinds of data that we use in programming. In JavaScript, there are seven fundamental data types:
- Number: Any number, including numbers with decimals (the set of all integers and floating point numbers) :
4
,8
,1516
,23.42
.
let amount = 6;
let price = 4.99;
-
String: They are any grouping of characters (letters, spaces, numbers, or symbols) surrounded by single quotes
'...'
or double quotes"..."
.
let single = 'Wheres my bandit hat?';
let double = "Wheres my bandit hat?";
-
Boolean: This data type only has two possible values— either
true
orfalse
(without quotes). It’s helpful to think of booleans as on and off switches or as the answers to a “yes” or “no” question.
let lateToWork = true;
-
Null: This data type represents the intentional absence of a value, and is represented by the keyword
null
(without quotes).
let x = null;
-
Undefined: This data type is lack of defined value. Variables that are declared but not initialized to a value will have the value
undefined
.
var a;
console.log(a);
// Prints: undefined
Symbol: A newer feature to the language, symbols are unique identifiers, useful in more complex coding. No need to worry about these for now.
Object: Collections of related data.
Arithmetic Operators
JavaScript supports arithmetic operators for:
-
+
addition -
-
subtraction -
*
multiplication -
/
division -
%
modulo
// Addition
5 + 5
// Subtraction
10 - 5
// Multiplication
5 * 10
// Division
10 / 5
// Modulo
10 % 5
String Concatenation
In JavaScript, multiple strings can be concatenated together using the +
operator. In the example, multiple strings and variables containing string values have been concatenated. After execution of the code block, the displayText
variable will contain the concatenated string.
let service = 'credit card';
let month = 'May 30th';
let displayText = 'Your ' + service + ' bill is due on ' + month + '.';
console.log(displayText);
// Prints: Your credit card bill is due on May 30th.
Properties
When you introduce a new piece of data into a JavaScript program, the browser saves it as an instance of the data type. Every string instance has a property called length
that stores the number of characters in that string.
let message = 'good nite';
console.log(message.length);
// Prints: 9
console.log('howdy'.length);
// Prints: 5
Libraries
Libraries contain methods that can be called by appending the library name with a period .
, the method name, and a set of parentheses.
Math.random();
// ☝️ Math is the library
Methods
Methods return information about an object, and are called by appending an instance with a period .
, the method name, and parentheses. You can find a list of built-in string methods in the JavaScript documentation. Developers use documentation as a reference tool. It describes JavaScript’s keywords, methods, and syntax.
The Math.random()
function returns a floating-point, random number in the range from 0 (inclusive) up to but not including 1.
console.log(Math.random());
// Prints: 0 - 0.9
The Math.floor()
function returns the largest integer less than or equal to the given number.
console.log(Math.floor(5.95));
// Prints: 5
Top comments (0)
Some comments have been hidden by the post's author - find out more