Essential JavaScript Cheat Sheet: Operators and Dates Explained
Written on
Chapter 1: Understanding JavaScript Basics
JavaScript ranks among the most widely utilized programming languages in web development. In this article, we will explore the essential syntax and operators used in modern JavaScript.
Arithmetic Operations
JavaScript allows us to perform basic arithmetic operations. For instance, we can add using + and subtract using - like so:
let a = b + c - d;
To multiply, we use *, and for division, we use /:
let a = b * (c / d);
To find the remainder of a division, we can use %:
let x = 100 % 48;
Additionally, we can increment a variable with ++:
a++;
And decrement with --:
b--;
Type Checking
To determine the type of primitive values and objects, we use the typeof operator:
typeof a
This is particularly useful for primitive values, as it returns 'object' for all objects.
Assignment Operations
We assign a value to a variable using =:
let x = 10;
The value on the right is stored in the variable on the left. For instance, a += b is a shorthand for a = a + b, and we can also use -, *, and / in a similar fashion.
Comparison Operators
Equality is checked using ===:
a === b
To check for inequality, we use !==:
a !== b
We can determine if a is greater than b with:
a > b
And if a is less than b with:
a < b
For less than or equal to, we use:
a <= b
And for greater than or equal to:
a >= b
Logical operators include AND (&&):
a && b
And OR (||):
a || b
Date Manipulation in JavaScript
We can create date objects using the Date constructor:
let d = new Date("2017-06-23");
If we skip the month and day, it defaults to January 1:
let d = new Date("2017");
You can also specify hours, minutes, and seconds as follows:
let d = new Date("2017-06-23T12:00:00-09:45");
For a more human-readable format, you can use:
let d1 = new Date("June 23 2017");
let d2 = new Date("Jun 23 2017 07:45:00 GMT+0100 (Tokyo Time)");
Date objects come with various methods to retrieve specific values:
let d = new Date();
let a = d.getDay(); // Gets the day of the week
let b = d.getDate(); // Gets the day of the month
let c = d.getFullYear(); // Gets the full year
let h = d.getHours(); // Gets the hours
let ms = d.getMilliseconds(); // Gets the milliseconds
let m = d.getMinutes(); // Gets the minutes
let mo = d.getMonth(); // Gets the month
let s = d.getSeconds(); // Gets the seconds
let t = d.getTime(); // Gets milliseconds since 1970-01-01
We can also set values using setter methods:
let d = new Date();
d.setDate(d.getDate() + 7); // Sets the date to one week later
The setter methods include:
- setDay(): Sets the day of the week
- setDate(): Sets the day of the month
- setFullYear(): Sets the full year
- setHours(): Sets the hours
- setMilliseconds(): Sets the milliseconds
- setMinutes(): Sets the minutes
- setMonth(): Sets the month
- setSeconds(): Sets the seconds
- setTime(): Sets the timestamp
Conclusion
JavaScript provides a range of operators and the Date constructor to create, retrieve, and manipulate dates effectively.
Enjoyed this article? For more informative content, consider subscribing to our YouTube channel!
The first video, The Only JavaScript Cheat Sheet You Need (For Beginners & Pros), offers a comprehensive guide for all skill levels, covering essential JavaScript concepts and tips.
The second video, 20: Date methods in JavaScript - Learn JavaScript front-end programming, provides an in-depth look at how to effectively work with dates in JavaScript, perfect for front-end developers.