The below code snippet works in Office 365 and SharePoint 2013 +
The below code used the SP.User.groups property to retrieve the collection of groups, which the current user is member. https://msdn.microsoft.com/en-us/library/office/jj244862.aspx
function getcurrentuserGroups() {
var clientContext = SP.ClientContext.get_current();
oUser = clientContext.get_web().get_currentUser();
oGroups = oUser.get_groups();
clientContext.load(oUser);
clientContext.load(oGroups);
clientContext.executeQueryAsync(
Function.createDelegate(this, function() {
var groupsInfo = '';
var groupsEnumerator = oGroups.getEnumerator();
console.log('Count of current user groups: ' + oGroups.get_count());
while (groupsEnumerator.moveNext()) {
var oGroup = groupsEnumerator.get_current();
groupsInfo += '\n' + 'Group ID: ' + oGroup.get_id() + ', ' + 'Title : ' + oGroup.get_title();
}
console.log(groupsInfo.toString());
}),
Function.createDelegate(this, function() {
console.log('failed');
}));
} Call the above method using,function injectMethod(){
getcurrentuserGroups();
}
ExecuteOrDelayUntilScriptLoaded(injectMethod, "sp.js");