JavaScript Cheat Sheet for Beginners: Essential Concepts and Syntax, Cheat Sheet of Javascript programming

JavaScript cheat sheet for beginners: Variables, Operators, Functions, If - Else Statements, Regular Expression Syntax and so on.

Typology: Cheat Sheet

2019/2020

Uploaded on 10/09/2020

amritay
amritay ๐Ÿ‡บ๐Ÿ‡ธ

4.7

(14)

256 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Beginnerโ€™s essential
JavaScript Cheat Sheet
The Language of the Web.
#################
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download JavaScript Cheat Sheet for Beginners: Essential Concepts and Syntax and more Cheat Sheet Javascript programming in PDF only on Docsity!

Beginnerโ€™s essential

JavaScript Cheat Sheet

The Language of the Web.

#################

TABLE OF CONTENTS

  • JavaScript Basics
  • Variables in JavaScript
  • The Next Level: Arrays
  • Operators
  • Functions
  • JavaScript Loop
  • If - Else Statements
  • Strings
  • Regular Expression Syntax
  • Numbers and Math
  • Dealing with Dates in JavaScript
  • DOM Mode
  • Working with the User Browser
  • JavaScript Events

True or fase statements โ€” var c = true Constant numbers โ€” const PI = 3. Objects โ€” var name = {firstName:"John", lastName:โ€Doe"}

Objects

var person = { firstName:"John", lastName:"Doe", age:20, nationality:"German" };

THE NEXT LEVEL: ARRAYS

var fruit = ["Banana", "Apple", "Pear"];

Array Methods

concat() โ€” Join several arrays into one indexOf() โ€” Returns the primitive value of the specified object join() โ€” Combine elements of an array into a single string and return the string lastIndexOf() โ€” Gives the last position at which a given element appears in an array pop() โ€” Removes the last element of an array push() โ€” Add a new element at the end reverse() โ€” Sort elements in descending order shift() โ€” Remove the first element of an array slice() โ€” Pulls a copy of a portion of an array into a new array

sort() โ€” Sorts elements alphabetically splice() โ€” Adds elements in a specified way and position toString() โ€” Converts elements to strings unshift() โ€” Adds a new element to the beginning valueOf() โ€” Returns the first position at which a given element appears in an array

OPERATORS

Basic Operators

  • โ€” Addition
  • โ€” Subtraction
  • โ€” Multiplication / โ€” Division (...) โ€” Grouping operator, operations within brackets are executed earlier than those outside % โ€” Modulus (remainder ) ++ โ€” Increment numbers -- โ€” Decrement numbers

Comparison Operators

== โ€” Equal to === โ€” Equal value and equal type != โ€” Not equal !== โ€” Not equal value or not equal type

โ€” Greater than < โ€” Less than = โ€” Greater than or equal to

document.write() โ€” Write directly to the HTML document prompt() โ€” Creates an dialogue for user input

Global Functions

decodeURI() โ€” Decodes a Uniform Resource Identifier (URI) created by encodeURI or similar decodeURIComponent() โ€” Decodes a URI component encodeURI() โ€” Encodes a URI into UTF- encodeURIComponent() โ€” Same but for URI components eval() โ€” Evaluates JavaScript code represented as a string isFinite() โ€” Determines whether a passed value is a finite number isNaN() โ€” Determines whether a value is NaN or not Number() โ€” Returns a number converted from its argument parseFloat() โ€” Parses an argument and returns a floating point number parseInt() โ€” Parses its argument and returns an integer

JAVASCRIPT LOOPS

for (before loop; condition for loop; execute after loop) { // what to do during the loop } for โ€” The most common way to create a loop in JavaScript while โ€” Sets up conditions under which aloop executes do while โ€” Similar to the while loop, however, it executes at least once and performs a check at the end to see if the condition is met to execute again break โ€” Used to stop and exit the cycle at certain conditions continue โ€” Skip parts of the cycle if certain conditions are met

IF - ELSE STATEMENTS

if (condition) { // what to do if condition is met } else { // what to do if condition is not met }

STRINGS

var person = "John Doe";

Escape Characters

' โ€” Single quote " โ€” Double quote \ โ€” Backslash \b โ€” Backspace \f โ€” Form feed \n โ€” New line \r โ€” Carriage return \t โ€” Horizontal tabulator \v โ€” Vertical tabulator

String Methods

charAt() โ€” Returns a character at a specified position inside a string charCodeAt() โ€” Gives you the unicode of character at that position concat() โ€” Concatenates (joins) two or more strings into one

x โ€” Allow comments and whitespace in pattern U โ€” Ungreedy pattern

Brackets

[abc] โ€” Find any of the characters between the brackets [^abc] โ€” Find any character not in the brackets [0-9] โ€” Used to find any digit from 0 to 9 [A-z] โ€” Find any character from uppercase A to lowercase z (a|b|c) โ€” Find any of the alternatives separated with |

Metacharacters

. โ€” Find a single character, except newline or line terminator \w โ€” Word character \W โ€” Non-word character \d โ€” A digit \D โ€” A non-digit character \s โ€” Whitespace character \S โ€” Non-whitespace character \b โ€” Find a match at the beginning/end of a word \B โ€” A match not at the beginning/end of a word \0 โ€” NUL character \n โ€” A new line character \f โ€” Form feed character \r โ€” Carriage return character \t โ€” Tab character \v โ€” Vertical tab character \xxx โ€” The character specified by an octal number xxx

\xdd โ€” Character specified by a hexadecimal number dd \uxxxx โ€” The Unicode character specified by a hexadecimal number xxxx

Quantifiers

n+ โ€” Matches any string that contains at least one n n* โ€” Any string that contains zero or more occurrences of n n? โ€” A string that contains zero or one occurrences of n n{X} โ€” String that contains a sequence of X nโ€™s n{X,Y} โ€” Strings that contains a sequence of X to Y nโ€™s n{X,} โ€” Matches any string that contains a sequence of at least X nโ€™s n$ โ€” Any string with n at the end of it ^n โ€” String with n at the beginning of it ?=n โ€” Any string that is followed by a specific string n ?!n โ€” String that is not followed by a specific string n

NUMBERS AND MATH

Number Properties

MAX_VALUE โ€” The maximum numeric value representable in JavaScript MIN_VALUE โ€” Smallest positive numeric value representable in JavaScript NaN โ€” The โ€œNot-a-Numberโ€ value NEGATIVE_INFINITY โ€” The negative Infinity value POSITIVE_INFINITY โ€” Positive Infinity value

Number Methods

toExponential() โ€” Returns a string with a rounded number written as exponential notation

max(x,y,z,...,n) โ€” Returns the number with the highest value min(x,y,z,...,n) โ€” Same for the number with the lowest value pow(x,y) โ€” X to the power of y random() โ€” Returns a random number between 0 and 1 round(x) โ€” The value of x rounded to its nearest integer sin(x) โ€” The sine of x (x is in radians) sqrt(x) โ€” Square root of x tan(x) โ€” The tangent of an angle

DEALING WITH DATES IN JAVASCRIPT

Setting Dates

Date() โ€” Creates a new date object with the current date and time Date(2017, 5, 21, 3, 23, 10, 0) โ€” Create a custom date object. The numbers represent year, month, day, hour, minutes, seconds, milliseconds. You can omit anything you want except for year and month. Date("2017-06-23") โ€” Date declaration as a string

Pulling Date and Time Values

getDate() โ€” Get the day of the month as a number (1-31) getDay() โ€” The weekday as a number (0-6) getFullYear() โ€” Year as a four digit number (yyyy) getHours() โ€” Get the hour (0-23) getMilliseconds() โ€” The millisecond (0-999) getMinutes() โ€” Get the minute (0-59) getMonth() โ€” Month as a number (0-11) getSeconds() โ€” Get the second (0-59) getTime() โ€” Get the milliseconds since January 1, 1970

getUTCDate() โ€” The day (date) of the month in the specified date according to universal time (also available for day, month, fullyear, hours, minutes etc.) parse โ€” Parses a string representation of a date, and returns the number of milliseconds since January 1, 1970

Set Part of a Date

setDate() โ€” Set the day as a number (1-31) setFullYear() โ€” Sets the year (optionally month and day) setHours() โ€” Set the hour (0-23) setMilliseconds() โ€” Set milliseconds (0-999) setMinutes() โ€” Sets the minutes (0-59) setMonth() โ€” Set the month (0-11) setSeconds() โ€” Sets the seconds (0-59) setTime() โ€” Set the time (milliseconds since January 1, 1970) setUTCDate() โ€” Sets the day of the month for a specified date according to universal time (also available for day, month, fullyear, hours, minutes etc.)

DOM MODE

Node Properties

attributes โ€” Returns a live collection of all attributes registered to and element baseURI โ€” Provides the absolute base URL of an HTML element childNodes โ€” Gives a collection of an elementโ€™s child nodes firstChild โ€” Returns the first child node of an element lastChild โ€” The last child node of an element nextSibling โ€” Gives you the next node at the same node tree level nodeName โ€” Returns the name of a node

lookupPrefix() โ€” Returns a DOMString containing the prefix for a given namespaceURI, if present normalize() โ€” Joins adjacent text nodes and removes empty text nodes in an element removeChild() โ€” Removes a child node from an element replaceChild() โ€” Replaces a child node in an element

Element Methods

getAttribute() โ€” Returns the specified attribute value of an element node getAttributeNS() โ€” Returns string value of the attribute with the specified namespace and name getAttributeNode() โ€” Gets the specified attribute node getAttributeNodeNS() โ€” Returns the attribute node for the attribute with the given namespace and name getElementsByTagName() โ€” Provides a collection of all child elements with the specified tag name getElementsByTagNameNS() โ€” Returns a live HTMLCollection of elements with a certain tag name belonging to the given namespace hasAttribute() โ€” Returns true if an element has any attributes, otherwise false hasAttributeNS() โ€” Provides a true/false value indicating whether the current element in a given namespace has the specified attribute removeAttribute() โ€” Removes a specified attribute from an element removeAttributeNS() โ€” Removes the specified attribute from an element within a certain namespace removeAttributeNode() โ€” Takes away a specified attribute node and returns the removed node setAttribute() โ€” Sets or changes the specified attribute to a specified value setAttributeNS() โ€” Adds a new attribute or changes the value of an attribute with the given namespace and name setAttributeNode() โ€” Sets or changes the specified attribute node

setAttributeNodeNS() โ€” Adds a new namespaced attribute node to an element

WORKING WITH THE USER BROWSER

Window Properties

closed โ€” Checks whether a window has been closed or not and returns true or false defaultStatus โ€” Sets or returns the default text in the statusbar of a window document โ€” Returns the document object for the window frames โ€” Returns all